Thursday, August 1, 2013

Set Random Wallpaper on your Desktop using Powershell


Hi,

We all love wallpapers. I and love them too.

Sometime they reflect our moods too. I have a huge collection of Wallpapers and i love to change my wallpapers in once or twice a week.

In the time of windows XP, i used to have a software which changed my wallpaper daily when i login to the laptop.

To be frank i never used to have a any desktop changer software in my windows7 laptop.

Few days ago i just write a small snippet code to change my wallpaper on daily basis.

How i do that ,

It is simple, Powershell + Registry and that all.

Download Link : http://gallery.technet.microsoft.com/scriptcenter/Set-Random-Wallpaper-Using-e79e423

Lets do it.

The below variable is storing the path to my Pictures. {you can change it to with yours ,, or you can use Join-Path $env:USERPROFILE  -ChildPath Pictures .}

$myPictureFolder = 'C:\Users\aman.dhally\Pictures\’

Now, i am running Get-ChildItem (dir) on our Picture folder, and using Get-Random cmdlet, i am choosing any single  wallpaper randomly.

$getRandomWallpaper = Get-ChildItem -Recurse $myPictureFolder |where {$_.Extension -eq ".jpg"}  | Get-Random -Count 1

Now we are using Set-ItemProperty to set the select wallpaper to the desktop.
To update the desktop we have to use rundll32.exe and to be frank, until we don’t run the  "

rundll32.exe user32.dll, UpdatePerUserSystemParameters

” code 3-4 time wallpaper wont change. or we reboot the laptop.

Set-ItemProperty -path 'HKCU:\Control Panel\Desktop\' -name wallpaper -value $getRandomWallpaper.FullName 
rundll32.exe user32.dll, UpdatePerUserSystemParameters
Completer Script
$myPictureFolder = 'C:\Users\AD\Pictures\WallPapers\IronMan'

# $myPictureFolder = Join-Path $env:USERPROFILE  -ChildPath Pictures
$getRandomWallpaper = Get-ChildItem -Recurse $myPictureFolder |where {$_.Extension -eq ".jpg"}  | Get-Random -Count 1
Set-ItemProperty -path 'HKCU:\Control Panel\Desktop\' -name wallpaper -value $getRandomWallpaper.FullName 

rundll32.exe user32.dll, UpdatePerUserSystemParameters  
rundll32.exe user32.dll, UpdatePerUserSystemParameters
rundll32.exe user32.dll, UpdatePerUserSystemParameters

Download Linkhttp://gallery.technet.microsoft.com/scriptcenter/Set-Random-Wallpaper-Using-e79e423


Note: I tested this on windows 7 32 bit only.
Thanks
Aman Dhally
clip_image001 clip_image002 clip_image003 clip_image005clip_image007

3 comments:

  1. Very nice, i liked the idea, but all my pictures are in the "Dropbox/Camera Uploads" folder.
    Also i have some "old" Windows XP installation, so i did a JPG->BMP conversion.
    Maybe you like my version.
    https://www.dropbox.com/s/0sq4qsp2f873oq6/Set-Random-Wallpaper-Dropbox.ps1

    ReplyDelete
    Replies
    1. Hi Richard,

      Welcome to the New Delhi PowerShell User Group Blg.

      I am glad that you like it. and i checked your SCRIPT and i liked it a lot. :)

      Thanks
      Aman

      Delete
  2. Works on win 7 64 Dual scrren setup, Well done on the code , but is there a way to make it "stretch" the image and not "tile" the image, or is that already in the code and i just missed it;

    Thanks

    ReplyDelete

Note: Only a member of this blog may post a comment.