Skip to content

Instantly share code, notes, and snippets.

@gurnec
Last active November 3, 2025 17:35
Show Gist options
  • Select an option

  • Save gurnec/1306bbd9d42bb93af59c7bbcb2a243ad to your computer and use it in GitHub Desktop.

Select an option

Save gurnec/1306bbd9d42bb93af59c7bbcb2a243ad to your computer and use it in GitHub Desktop.
Set the current Windows wallpaper to Bing's image-of-the-day
$ErrorActionPreference = 'Stop'
if ((Test-Path "$PSCommandPath.autoupdate") -and ! (Test-Path "$PSCommandPath.config")) {
Set-Content "$PSCommandPath.config" '{ "autoupdate": true }'
Remove-Item "$PSCommandPath.autoupdate"
}
$config = Get-Content -Raw "$PSCommandPath.config" -ea Ignore | ConvertFrom-Json
cd ~
if (! (Test-Path -PathType Container Wallpapers)) { mkdir Wallpapers }
cd Wallpapers
$last = Get-ChildItem *.jpg, *.jpeg | Sort-Object LastWriteTime -Descending | Select-Object -First 1
if ($last -and [datetime]::Now - $last.LastWriteTime -lt '6:00:00') {
exit
}
while (! (Test-Connection 1.1 -Count 1 -Quiet)) { Start-Sleep 1 }
if ($config.autoupdate -and $args[0] -ne '-noupdate') {
$gistid = '1306bbd9d42bb93af59c7bbcb2a243ad'
$gistfn = 'Set-BingWallpaper.ps1'
$gist = Invoke-RestMethod https://api.github.com/gists/$gistid
if (([datetime]$gist.updated_at).ToUniversalTime() -gt (gi $PSCommandPath).LastWriteTimeUtc -and $gist.files.$gistfn.truncated -eq $false) {
Set-Content $PSCommandPath $gist.files.$gistfn.content
& $PSCommandPath -noupdate
exit
}
}
$today = (Invoke-RestMethod 'https://www.bing.com/HPImageArchive.aspx?format=js&n=1').images[0]
if (! $today) {
Write-Error 'HPImageArchive API returned no results'
}
if ($today.startdate -notmatch '^(\d{4})(\d\d)(\d\d)') {
Write-Error "Unexpected image startdate: $($today.startdate)"
}
$fn = ($Matches[1,2,3] -join '-') + '.jpg'
$url = 'https://www.bing.com/' + $(if ($config.urlbase_suffix) {
$today.urlbase + $config.urlbase_suffix
} else {
$today.url
})
Invoke-WebRequest $url -OutFile $fn
$setwallpapersrc = @'
using System.Runtime.InteropServices;
public class Wallpaper {
public const int SetDesktopWallpaper = 0x14;
public const int UpdateIniFile = 0x01;
public const int SendWinIniChange = 0x02;
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
public static int SetWallpaper(string path) {
return SystemParametersInfo(SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange);
}
}
'@
Add-Type -TypeDefinition $setwallpapersrc
if ([Wallpaper]::SetWallpaper("$PWD\$fn") -eq 0) { Write-Error 'SystemParametersInfo() returned false' }
Get-ChildItem *.jpg, *.jpeg | Sort-Object CreationTime -Descending | Select-Object -Skip 14 | Remove-Item
if ($today.copyright -and (Get-Command New-BurntToastNotification -ea Ignore)) {
$common = @{Silent = $true; UniqueId = 'SetBingWallpaper'; AppLogo = '\null.jpg'; wa = 'SilentlyContinue'}
if ($today.copyright -match '(.*)\((\u00a9.*)\)') {
New-BurntToastNotification -Text $Matches[1] -Attribution $Matches[2] @common
} else {
New-BurntToastNotification -Text $today.copyright @common
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment