Change the desktop wallpaper from PowerShell

There's no Set-Wallpaper cmdlet, but a tiny P/Invoke of SystemParametersInfo does it natively — the forgotten win32 call behind every 'set background' script.

WindowsdesktopmediapowershellLow risk
Run the Add-Type P/Invoke snippet, then call `[Win32.W]::SystemParametersInfo(20, 0, '<image path>', 3)` to set the wallpaper immediately.

01Command

Command
$ Add-Type -TypeDefinition 'using System;using System.Runtime.InteropServices;public class W{[DllImport("user32.dll",SetLastError=true)]public static extern bool SystemParametersInfo(int uAction,int uParam,string lpvParam,int fuWinIni);}' -Name W -Namespace Win32; [Win32.W]::SystemParametersInfo(20, 0, 'C:\Pictures\bg.jpg', 3)

02What it does

20 is SPI_SETDESKWALLPAPER and 3 combines SPIF_UPDATEINIFILE with SPIF_SENDCHANGE, which both saves the setting and broadcasts it so the desktop updates instantly. Because it's a P/Invoke, the command is longer than typical cmdlets, but it has zero dependencies and works in Windows PowerShell and PowerShell 7 alike. Some picture formats (e.g. HEIC) are rejected — convert to JPG/PNG first — and wallpaper 'fit' settings stay whatever the user chose in Settings.

03Risk

RiskLow risk
RequirementsReturns True on success; the image should be a format Windows wallpapers accept (JPG/PNG/BMP). Change takes effect immediately.

04Undo

✓No undo needed — this command does not change persistent state.

05Sources

Last verified: 2026-09-17 · Category: Media

06Related solutions