How do I get a Powershell script to set Wallpaper?

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 15 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
jackcraig
Posts: 18
Last visit: Mon Oct 20, 2008 9:55 pm

How do I get a Powershell script to set Wallpaper?

Post by jackcraig »

Hi,

first post here (and just tipping my toe into the Powershell waters!)

basically I'm trying to get a script that will update the wallpaper on my desktop - ideally I'd like to specify a folder and the script to change the wallpaper to one of the images in the folder for say 5 secs then update again. Kinda like a screen saver but using the wallpaper feature.

This is just for fun - I thought I'd use this idea as a vehicle to play around with powershell.

So far have found the IActiveDesktop COM interface which appears as if it might do the job but can't work out how to use this in a powershell script. I've also found out about the WMI class Win32_Desktop (from memory I think it was called that) but the wallpaper property appears to be read-only.

Sorry if this is a bit vague but it's my first day back at work after a two week break and my brain is not fully functional yet!

Any help much appreciated.

jack craig
User avatar
jackcraig
Posts: 18
Last visit: Mon Oct 20, 2008 9:55 pm

How do I get a Powershell script to set Wallpaper?

Post by jackcraig »

Hi,

first post here (and just tipping my toe into the Powershell waters!)

basically I'm trying to get a script that will update the wallpaper on my desktop - ideally I'd like to specify a folder and the script to change the wallpaper to one of the images in the folder for say 5 secs then update again. Kinda like a screen saver but using the wallpaper feature.

This is just for fun - I thought I'd use this idea as a vehicle to play around with powershell.

So far have found the IActiveDesktop COM interface which appears as if it might do the job but can't work out how to use this in a powershell script. I've also found out about the WMI class Win32_Desktop (from memory I think it was called that) but the wallpaper property appears to be read-only.

Sorry if this is a bit vague but it's my first day back at work after a two week break and my brain is not fully functional yet!

Any help much appreciated.

jack craig
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

How do I get a Powershell script to set Wallpaper?

Post by jvierra »

This sets thet wallpaper value:

Code: Select all

	
set-itemproperty -path "HKCU:Control PanelDesktop" -name WallPaper -value Zapotec.bmp
	
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

How do I get a Powershell script to set Wallpaper?

Post by jvierra »

For fun here is a solution:



uploads/2491/SetWallpaper.txt
User avatar
jackcraig
Posts: 18
Last visit: Mon Oct 20, 2008 9:55 pm

How do I get a Powershell script to set Wallpaper?

Post by jackcraig »

thanks jvierra for the SetWallpaper.txt, I will try that tonight - yep powershell does seem fun, seems a lot more powerfull and intuitive than anything else I've used to date...
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

How do I get a Powershell script to set Wallpaper?

Post by jvierra »

It is teh "cat's pajamas" of scripting in teh Windows World. I understand that Apple and Unix are negotiating for license rights. The full movie should be coming out in June. Stay tuned.

I left the timeout as an excercise for you. The method is not really related to Powerhell but is a technique that is around for vbscript and command files so I just wrapped them to kinda show how neat PowerShell can be.


User avatar
jackcraig
Posts: 18
Last visit: Mon Oct 20, 2008 9:55 pm

How do I get a Powershell script to set Wallpaper?

Post by jackcraig »

uploads/27955/wallpaper.txt


I've made two changes to the code example in the file helpfully posted by jvierra - I changed the use of $file.Name to $file.FullName and also modified the key "Wallpaper" in HKCU:SoftwareMicrosoftInternet ExplorerDesktopGeneral (Not sure if that bit was necessary)

Anyway it works and its all good fun!

One thing I'm going to look at is if I can restrict the area that gets redrawn as currently the desktop icons I have get redrawn even though they don't overlap the images I'm using (the images are only about 1000x750 pixels set on a black background). I'll post anything I find out on this.

Thanks again for the headstart jvierra
User avatar
jhicks
Posts: 1789
Last visit: Mon Oct 19, 2015 9:21 am

How do I get a Powershell script to set Wallpaper?

Post by jhicks »

Here's my contribution to the cause:

Code: Select all

function set-wallpaper{
    Param ( [string]$newpaper="",[string]$format)
    
    switch ($format) {
        "tiled" {$tile=1;$style=0}
        "centered" {$tile=0;$style=0}
        "fit" {$tile=0;$style=2}
        default {$tile=0;$style=0}
    }        
    
    #verify file exists
    if (Get-ChildItem $newpaper -ea "SilentlyContinue") {
       $regkeys="HKCU:Control PanelDesktop","HKCU:SoftwareMicrosoftInternet ExplorerDesktopGeneral"
       foreach ($regkey in $regkeys) {
        Set-ItemProperty -path $regkey -name WallPaper -value $newpaper
        Set-ItemProperty -path $regkey -name TileWallpaper -value $tile
        Set-ItemProperty -path $regkey -name WallpaperStyle -value $style
       }
    $rundll32="{0}System32RUNDLL32.EXE user32.dll, UpdatePerUserSystemParameters" -f  $env:windir
    Invoke-Expression -Command $rundll32
     }
     else {
        Write-Warning "Failed to find $newpaper"
     }
     
}
User avatar
martyix
Posts: 9
Last visit: Sat Sep 20, 2008 6:41 pm

How do I get a Powershell script to set Wallpaper?

Post by martyix »

And this way you can automatically change the wallpaper.. I was thinking about running the script after start of windows :-)

Code: Select all

function set-wallpaper([string]$newpaper="", [string]$format){
    
    echo "test"
    switch ($format) {
        "tiled" {$tile=1;$style=0}
        "centered" {$tile=0;$style=0}
        "fit" {$tile=0;$style=2}
        default {$tile=0;$style=0}
    }        
    
    #verify file exists
    if (Get-ChildItem $newpaper -ea "SilentlyContinue") {
       $regkeys="HKCU:Control PanelDesktop","HKCU:SoftwareMicrosoftInternet ExplorerDesktopGeneral"
       foreach ($regkey in $regkeys) {
        Set-ItemProperty -path $regkey -name WallPaper -value $newpaper
        Set-ItemProperty -path $regkey -name TileWallpaper -value $tile
        Set-ItemProperty -path $regkey -name WallpaperStyle -value $style
       }
    $rundll32="{0}System32RUNDLL32.EXE user32.dll, UpdatePerUserSystemParameters" -f  $env:windir
    Invoke-Expression -Command $rundll32
     }
     else {
        Write-Warning "Failed to find $newpaper"
     }   
}

$folder="D:FunFrancie2008"

$file=get-Childitem $folder -name | get-Random

set-wallpaper $folder/$file "fit"
martyix2008-09-05 06:50:48
This topic is 15 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked