This is a place for Systems Administrators and IT Professionals to find and share ideas, solutions and templates. If you have something that helps you solve a problem, chances are it will help someone else too. So pay it forward and send an email to TheAgreeableCow at gmail. Full mudos to you!

Monday 8 September 2014

Set a Desktop Wallpaper using PowerShell

This script will apply a desktop wallpaper from a variety of sources and optionally overlay some text, using PowerShell.

The wallpaper sources include:
  • A solid colour (the "no wallpaper" option)
  • A specific or random picture from a directory
  • A Google Image search

The Text Overlay feature provides optional BGInfo style text directly onto the wallpaper image with control over the content, font, size, colour and position.

The script can be run manually, at logon or even repeatedly via a scheduled task to update the wallpaper regularly.

Wallpaper example showing the text overlay


The Shell syntax is very straight forward
        Set-Wallpaper [Source] [Selection] 
    MyPics Examples
        .\Set-Wallpaper.ps1 MyPics *
        .\Set-Wallpaper.ps1 MyPics coolpic.jpg
    Web Example
        .\Set-Wallpaper.ps1 Web 'Ayers Rock'

    Colour Example
        .\Set-Wallpaper.ps1 Colour Blue

Please note,  Powershell v3 or later is required due to the invoke-webrequest cmdlet in the Web module.

Setup Options

All of the options in the script can be set via the Wallpaper Variables section.

# MyPics Options
[STRING]$PicturesPath = [environment]::getfolderpath("MyPictures")+"\wallpaper"
[BOOLEAN]$ResizeMyPics = $False  

# Web Options
[INT]$MaxResults = 10
[INT]$DaysBetweenSearches = 7
[BOOLEAN]$ResizeWebPics = $True
[STRING]$WebProxyServer = ""

# Text Overlay Options
[BOOLEAN]$TextOverlay = $True   
[STRING]$TextColour = "White"
[STRING]$FontName = "Arial"
[INT]$FontSize = 14
[BOOLEAN]$ApplyHeader = $True
[STRING]$TextAlign = "Right"
[STRING]$Position = "High"     

# Wallpaper Style Options
[STRING]$Style = "Fit"         

# Available Colours
$Grey = @(192,192,192)
$Black = @(0,0,0)
$White = @(255,255,255)
$Red = @(220,20,60)
$Green = @(0,128,0)
$Yellow = @(255,255,0)
$Blue = @(0,0,255)
$CornflourBlue = @(100,149,237)

My Pictures Wallaper

The 'MyPics' option will source pictures from a nominated folder ('My Pictures\Wallpaper' by default). You can either choose a specific picture or a wildcard *. Using the wildcard with a scheduled task allows the wallpaper to change regularly. 

Pictures can optionally be re-sized proportionately to match the screen resolution.

Google Images Wallpaper

The 'Web' option will perform a Google image search based on your search term and automatically download a number of high resolution pictures. These are then randomly chosen as the desktop wallpaper. Again the script can be run again manually or via a scheduled task to rotate between the downloaded pictures. 

To avoid repeated downloads each time the script runs with the same search term, simply adjust the $DaysBetweenSearches variable. By default, the script will only repeat a search after a week.

As the images can vary greatly in size, using the re-size variable is recommended here as it will keep the image (and any text overlays) consistent.

Typical download results using the syntax:   set-wallpaper Web 'mountains'

Colour Wallpaper

The solid colour wallpaper is the most straight forward and is essentially a "no wallpaper" option. The result is a plain background as the script simply draws a rectangle in the colour of your choice, at the same resolution of the screen. Extra colours can be added by updating a new variable with the relevant RGB values.

Text Overlay

Mudos for the text overlay proof of concept found on this post, which combines text and your chosen desktop. The text can be anything you like, including dynamic data sourced through powershell and wmi queries such as the computer name, OS or boot time.

There are a number of variables that control the font and where about's on the image the text is placed.

Example Text Overlay on a plain colour wallpaper


There are a number of included functions that tie all of this together. 
  • Get-MyImages chooses pictures for the 'MyPics' option
  • Set-WebProxy allows the Google search connection through a web proxy
  • Get-GoogleImages downloads and chooses pictures for the 'Web' option
  • Set-ImageSize optionally re-sizes pictures proportionately to match screen resolution
  • New-Wallpaper creates the actual bitmap (colour or picture) and combines the text overlay
  • Update-Wallpaper applies the actual wallpaper to your desktop
  • Set-Wallpaper validates the pipeline input and initiates all of the other sub-functions as required

Here's the full script as posted on Git Hub (select the 'Raw' option at the bottom to copy/paste)

         (oo)  ok
   /------\/  /
  / |    ||
 *  /\---/\
    ^^   ^^


  1. this is awesome! Thank you for sharing :D

    One problem I seem to have is setting $TextOverlay to $False does not turn off the overlay. Sill awesome! :D

  2. So, just discovered this, and I've been looking for something like this for a while. Can I just apply the text to my existing background without updating the image? I already use a background updater and don't want to stop using that.


    1. Not in its current format, the two would conflict.

  3. Great article thanks. Any way to apply this to AD groups? So in other words if a user is part of the "cool desktop image" group, I can apply a specific image for that group and a different one for the "stupid desktop image" group? Thanks for any insight and help you can provide.


  4. I am trying to use this script at logon from the startup folder, but I cant get the style to work. I am using "fit", but the wallpaper always get set to "fill".
    Any Ideas what is wrong?

  5. And why do I have to use the -noexit parameter in the shortcut when running from the startup folder?
    And can you explain the use of all the characters in the line? ".'"c:\MyScripts\Set-Wallpaper.ps1 Colour Blue'"

  6. korean wallpaper are the latest to hit the market and are gaining popularity fast

  7. They have the latest processors, a more than decent amount of system RAM and some of the best video cards money can buy. True hardcore gaming desktops even go so far as to having SSD hard drives installed, which guarantee an unparalleled level of speed and durability.Best 240HZ Monitor:[G-Sync, FreeSync, Budget] 2019 Updated

  8. I am getting this error:
    Cannot invoke method. Method invocation is supported only on core types in this language mode.
    At P:\Documents\scripts\PowerShell\SetBackground.ps1:154 char:1
    + [STRING]$PicturesPath = [environment]::getfolderpath("MyPictures")+"\ ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodInvocationNotSupportedInConstrainedLanguage