Skip to content

Instantly share code, notes, and snippets.

@s7ephen
Created November 24, 2010 17:20
Show Gist options
  • Save s7ephen/714023 to your computer and use it in GitHub Desktop.
Save s7ephen/714023 to your computer and use it in GitHub Desktop.
How to change the desktop wallpaper from powershell.
set-itemproperty -path "HKCU:Control Panel\Desktop" -name WallPaper -value accipiter.png
@ykhan21
Copy link

ykhan21 commented Jan 30, 2023

@gabrielkryss You can put BirkhoffLee's post above in a ps1 file like so:

# Note: if scheduling this script, you may need to check 
# that the powershell session is logged in as the user

$path = $args[0]

$setwallpapersrc = @"
using System.Runtime.InteropServices;

public class Wallpaper
{
  public const int SetDesktopWallpaper = 20;
  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 void SetWallpaper(string path)
  {
    SystemParametersInfo(SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange);
  }
}
"@
Add-Type -TypeDefinition $setwallpapersrc

[Wallpaper]::SetWallpaper($path)

And call it like so:

wallpaper.ps1 D:\wallpapers\wallpaper.jpg

Or you could keep your wallpaper at a specific location (e.g., $env:LOCALAPPDATA\wallpaper.png) and have your script simply set that path as the wallpaper.

@Mister-Curious
Copy link

Is there a way to get these awesome scripts to work for an entire DIR, instead of single file?

Cheers

@Arsenius01
Copy link

$setwallpapersrc = @"

...

Thank you very much, perfectly working with powershell, amazing :o

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment