0

I am trying to use PowerShell script to set windows wallpaper. I learned it from Script to change wallpaper in windows 10? :

Function Set-WallPaper($Value)
{
    Set-ItemProperty -path 'HKCU:\Control Panel\Desktop\' -name wallpaper -value $value
    rundll32.exe user32.dll, UpdatePerUserSystemParameters
}
 
Set-WallPaper -value 'c:\Temp\wallpaper.png'

Unfortunately it works only if Settings -> Personalization -> Background is already set to "Picture". It doesn't switch from "Solid Color". Is there a way to extend the script, so it switches from "Solid Color" to "Picture", too?

jing
  • 1,919
  • 2
  • 20
  • 39
  • 1
    Even if you logout and log back in it doesn't? – MF- May 05 '21 at 20:33
  • Good question. If I log out and in, the wallpaper is set as needed. So the registry value is good, but I need to propagate the change somehow. Hmm. – jing May 06 '21 at 05:40
  • From what I've read some have been successful changing the image simply by running the script two times. Another interesting method I saw was to change your screen's resolution. As for scripting it - the script does change the key as you had acknowledged so it seems to be a bug. – MF- May 06 '21 at 12:50

1 Answers1

-1

OK, so solution is to use SystemParametersInfo method instead of the legacy and unreliable UpdatePerUserSystemParameters. E.g. this form from Set-Wallpaper by Jose Espitia

Function Set-WallPaper($Image) {
  
Add-Type -TypeDefinition @" 
using System; 
using System.Runtime.InteropServices;
  
public class Params
{ 
    [DllImport("User32.dll",CharSet=CharSet.Unicode)] 
    public static extern int SystemParametersInfo (Int32 uAction, 
                                                   Int32 uParam, 
                                                   String lpvParam, 
                                                   Int32 fuWinIni);
}
"@ 
  
    $SPI_SETDESKWALLPAPER = 0x0014
    $UpdateIniFile = 0x01
    $SendChangeEvent = 0x02
  
    $fWinIni = $UpdateIniFile -bor $SendChangeEvent
  
    $ret = [Params]::SystemParametersInfo($SPI_SETDESKWALLPAPER, 0, $Image, $fWinIni)
 
}
 
 Set-WallPaper -Image 'c:\Temp\wallpaper.png'
jing
  • 1,919
  • 2
  • 20
  • 39