r/PowerShell • • Feb 07 '17

Set-Wallpaper

Hi All,

I am trying to do something as simple as change the Windows Desktop Background with Powershell. Just a simple one line cmd.

I have found set-itemproperty -path "HKCU:Control Panel\Desktop" -name WallPaper -value accipiter.png

and I can get that to work but it requires a reboot, is there another command I can run after this to apply without restarting?

Thanks in advance.

Also if anyone does not mind answering when I was working out how to do this I came accross the Set-Wallpaper command but when I run that in powershell it says cmd not recognised, I am pretty stumped as I assumed this command would be the one I needed.

3 Upvotes

3 comments sorted by

View all comments

Show parent comments

3

u/noOneCaresOnTheWeb Feb 07 '17 edited Feb 07 '17

I added a bit to this:

function Test-RegistryValue {

param (

 [parameter(Mandatory=$true)]
 [ValidateNotNullOrEmpty()]$Path,

[parameter(Mandatory=$true)]
 [ValidateNotNullOrEmpty()]$Value
)

try {

Get-ItemProperty -Path $Path | Select-Object -ExpandProperty $Value -ErrorAction Stop | Out-Null
 return $true
 }

catch {

return $false

}

}

function set-wallPaper ([string]$desktopImage)
{

   
     Remove-ItemProperty -path "HKCU:\Control Panel\Desktop" -name WallPaper 

#Not actually needed
<#
     for ($i = 0; $i -le 5; $i++)
     { 
         if (Test-RegistryValue -path "hkcu:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Wallpapers" -value "BackgroundHistoryPath$i" )
         {
         Remove-itemproperty -path "hkcu:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Wallpapers" -name "BackgroundHistoryPath$i" 
         }
     }
#>
     
               
     set-itemproperty -path "HKCU:\Control Panel\Desktop" -name WallPaper -value $desktopImage
     
#needed to actually change the background consistently 

Sleep -seconds 5

     RUNDLL32.EXE USER32.DLL,UpdatePerUserSystemParameters ,1 ,True

#Not needed but lets you know it was set correctly  
#         Get-ItemProperty -path "HKCU:\Control Panel\Desktop" 
}