Hey, Scripting Guy! How Can I Hide My Desktop Wallpaper?

Hey, Scripting Guy! How Can I Hide My Desktop Wallpaper?

  • Comments 8
  • Likes

Hey, Scripting Guy! Question

I give a lot of presentations, and the background picture of my baby alpaca, while fascinating to me, probably does not have the same level of attraction to my clients. I know I can mouse around and change the desktop picture to some bland color, but I prefer to run a script and make the change because I can have the same script close Office Outlook and launch my presentation slides as well. I know how to use the Stop-Process cmdlet to stop Office Outlook, and it is a no-brainer to start my PowerPoint slide show. However, I am stumped at changing the background picture of my baby alpaca. I tried to use WMI, but the Win32_Desktop class does not seem to allow updates. Do you have any suggestions?

- JP

SpacerHey, Scripting Guy! Answer

Hi JP,

I have a great suggestion—leave the alpaca desktop. I think they are great pets. I fell in love with them when I was working in Peru. I have a blanket made from baby alpaca fur that I use when I am reading beside the fireplace in the evening. Besides, alpacas are very polite animals and have wonderful dispositions. But because you have already decided to hide the alpaca during presentations, I guess I will show you how to use the registry to make the required changes. (Now if you had said you were getting rid of a picture of a cat, I would willingly help you. As it is, I am helping under protest.)

This week is Desktop Week-a rather vague series of loosely connected articles that may or may not be related to desktops. But the articles were written on a desktop, so maybe that counts. If you want to see some VBScript examples of Desktop Management scripts, check out these scripts in the Script Center Script Repository. The Community-Submitted Scripts Center also has a collection of scripts related to desktop management. In addition, there is the Desktop Management archive of “Hey Scripting Guy!” articles, which include scripts and explanations as well. The scripts this week will be using Windows PowerShell. To download Windows PowerShell and to find getting started information, see the Windows PowerShell hub.

The Get-SetDesktopBackground.ps1 script will allow you to retrieve the desktop background picture via the registry. It will also allow you to set the desktop background picture as well. The Get-SetDesktopBackground.ps1 script is seen here.

Get-SetDesktopBackground.ps1

Function Get-WallPaper()
{
 $wp=Get-ItemProperty -path 'HKCU:\Control Panel\Desktop\' -name wallpaper
 if(!$wp.WallPaper) 
   { "Wall paper is not set" }
 Else
  {"Wall paper is set to $($wp.WallPaper)" }
}

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

Get-WallPaper
Set-WallPaper -value ""
Get-WallPaper

In the Get-SetDesktopBackground.ps1 script, we begin with a function that retrieves the background information from the registry. The registry key is seen here:

Image of the registry key

 

To read the value from the registry, we can use the Get-ItemProperty cmdlet from Windows PowerShell. When using the Get-ItemProperty cmdlet, we need to give it the path to the registry key and the name of the registry key property to retrieve. The cool thing about Windows PowerShell is there is a thing that is called a registry provider. The registry provider exposes the registry as if it were a drive. You can type dir HKCU:\ if you wish, and you will obtain a directory listing of registry keys. It is way cool (not merely plain old cool)!

If we were to use the Get-ItemProperty cmdlet to retrieve the wallpaper settings directly, we would retrieve a custom Windows PowerShell object that contains a lot of information about the registry key, as well as the property value. This is seen here:

PS C:\> Get-ItemProperty -path 'HKCU:\Control Panel\Desktop\' -name wallpaper


PSPath       : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Control Panel\Desktop\
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Control Panel
PSChildName  : Desktop
PSDrive      : HKCU
PSProvider   : Microsoft.PowerShell.Core\Registry
Wallpaper    : C:\Windows\zapotec.bmp

We are only interested in the value of the wallpaper setting. We store this custom object in the $wp variable, and then we check to see if there is a value for the wallpaper. If there is no entry in the registry key, there is no wallpaper set. This is seen here:

Image of no wallpaper being set

 

If there is a value for the registry key property, it is displayed. The complete Get-WallPaper function is seen here:

Function Get-WallPaper()
{
 $wp=Get-ItemProperty -path 'HKCU:\Control Panel\Desktop\' -name wallpaper
 if(!$wp.WallPaper) 
   { "Wall paper is not set" }
 Else
  {"Wall paper is set to $($wp.WallPaper)" }
}

To set the wallpaper, we use Set-ItemProperty instead of Get-ItemProperty. We again specify the path to the registry key, the name of the registry key property, and the new value we wish to set. The new value is supplied when the Set-WallPaper function is called. To ensure the changes to the wallpaper take place immediately, we call the rundll32.exe program and we feed it the user32.dll. We call the UpdatePerUserSystemParameters method to cause the new registry entry to take effect. This works great for setting system wallpapers, and turning off wallpapers. Depending on the image file you are working with, your success in getting this method to work will vary. The complete Set-WallPaper function is seen here:

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

To change the wall paper to zapotec.bmp, you would pass the path to the zapotec.bmp file when calling the function:

Set-WallPaper -value "C:\Windows\zapotec.bmp"

If you want to turn off the wall paper altogether, as was expressed in the e-mail we got from JP, you would call the function and pass it a set of empty quotation marks as is seen here:

Set-WallPaper –value ""

JP, that is all there is to getting and setting desktop wallpaper via the registry. This also concludes our Desktop Week articles. Join us tomorrow for Quick-Hits Friday, where we will tackle an assortment of short questions using both VBScript and Windows PowerShell in our answers. Until then, grab your alpaca blanket and stay warm.

 

Ed Wilson and Craig Liebendorfer, Scripting Guys

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Here are the keys I have had to modify to get what I want...

    HKCU:\Control Panel\Desktop\wallpaper - technet.microsoft.com/.../cc978625.aspx

    HKCU:\Control Panel\Desktop\Tilewallpaper - technet.microsoft.com/.../cc978623.aspx

    HKCU:\Control Panel\Desktop\wallpaperstyle - technet.microsoft.com/.../cc978626.aspx

  • When I try this on my WinXP laptop, the screen does not change, but if I open the Display Properties I see the newly selected wallpaper.  The desktop does not refresh to show the new wallpaper.  Is there another command to force a redraw?

  • Derek, if you're doing this with JPEGs, you may have to kill explorer and start it again to take effect.  Bitmaps, on the other hand, work fine.

  • My registry is edited correctly with the full path to the image, but my Wallpaper is not changing... It seems that something is not getting refreshed. I have even rebooted. Is there something in Windows 7 that is not letting this work?

  • Thank you for the link. I’m on my way to check it out and pretty up my desktop.

    <a href="ac-digital.com/.../a>

  • The Registry Wallpaper gets set in Registry but the desktop doesnt refresh to the new Wallpaper. Is there a difference with W7 64 BIT?

    I think  rundll32.exe user32.dll, UpdatePerUserSystemParameters

    isnt working?!

  • Bulli, it's probably the 64 bit redirection with the registry. Try it in an x86 powershell session. I haven't figured out exactly what it is yet though.