Skip to content

Instantly share code, notes, and snippets.

@s7ephen
Created November 24, 2010 17:20
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • 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
@OXDBXKXO
Copy link

OXDBXKXO commented Jan 6, 2017

Thx ;)

@nolsen311
Copy link

Any ideas on how to force windows to recognize the change (Windows 7 sp1 x64)

@cventour
Copy link

rundll32.exe user32.dll, UpdatePerUserSystemParameters

@BirkhoffLee
Copy link

$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("C:\wallpaper.png")

@gabrielkryss
Copy link

Is there an updated version of this command or is the consensus in 2022 (going into 2023) for changing wallpapers from PowerShell is to never do it and just use the UI to do it? I kind of want to be able to change my wallpaper from telescope.nivm and the first step is to be able to do it in PowerShell. Can I get a feel on what the opinions on this

@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