Last active
February 13, 2026 17:37
-
-
Save gurnec/1306bbd9d42bb93af59c7bbcb2a243ad to your computer and use it in GitHub Desktop.
Set the current Windows wallpaper to Bing's image-of-the-day
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $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 | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This should work in either Windows Powershell 5.1 (pre-installed on Win10+) or Powershell 7+. JPGs are saved in a
Wallpaperssubdirectory at the script's location (up to 14 days' worth, older ones are deleted). The default download resolution is (currently, Microsoft can change this) 1920x1080. To choose a different one, create a JSON config file namedSet-BingWallpaper.ps1.configand place it in the same directory as the script. E.g. for 3840x2160, the config file should contain:{ "urlbase_suffix": "_UHD.jpg" }Here are the available
urlbase_suffixsettings:You can also enable auto-update mode (as in auto-update the script, not the wallpaper; use Task Scheduler to run the script daily), though it's not recommended unless you really trust this Internet stranger:
{ "autoupdate": true, "urlbase_suffix": "_UHD.jpg" }If the
BurntToastPowerShell module is installed, a notification annotating the image is displayed.