How to Change Wallpaper with PowerShell?

As an IT administrator, you might be required to change the system wallpaper. PowerShell is one of the best options to do this. In this tutorial, I will explain how to change the desktop wallpaper using PowerShell.

Change Wallpaper with PowerShell

To change the desktop wallpaper using PowerShell, we will use the SystemParametersInfo function from the user32.dll library. This function allows us to change system-wide parameters, including the desktop wallpaper.

Follow the below steps:

Step 1: Set Execution Policy

First, ensure that your system allows the execution of PowerShell scripts. Open PowerShell with administrative privileges and run the following command:

Set-ExecutionPolicy RemoteSigned

This command sets the execution policy to RemoteSigned, allowing scripts downloaded from the internet to run if they are signed by a trusted publisher.

Step 2: Create the PowerShell Script

Create a new PowerShell script file with a .ps1 extension. For example, you can name it Change-Wallpaper.ps1. Open your favorite text editor (like Notepad or Visual Studio Code) and paste the following script:

# Define the path to the new wallpaper
$wallpaperPath = "C:\Users\Public\Pictures\Sample Pictures\USA_Wallpaper.jpg"

# Load user32.dll and define the SystemParametersInfo function
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class User32 {
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
}
"@

# Set the wallpaper
$SPI_SETDESKWALLPAPER = 0x0014
$SPIF_UPDATEINIFILE = 0x01
$SPIF_SENDCHANGE = 0x02

User32::SystemParametersInfo($SPI_SETDESKWALLPAPER, 0, $wallpaperPath, $SPIF_UPDATEINIFILE -bor $SPIF_SENDCHANGE)

This script sets the desktop wallpaper to the specified image located at C:\Users\Public\Pictures\Sample Pictures\USA_Wallpaper.jpg. Make sure to replace this path with the actual path to your desired wallpaper image.

Step 3: Execute the Script

Save the script and run it in PowerShell. Navigate to the directory where your script is saved and execute the following command:

.\Change-Wallpaper.ps1

This command runs the script, changing your desktop wallpaper to the specified image.

Check out Create a Shortcut to a Folder Using PowerShell

Change Wallpaper on Multiple Systems using PowerShell

If you manage multiple systems, you can use PowerShell Remoting to change the wallpaper on remote machines. Ensure that PowerShell Remoting is enabled on all target systems. You can enable it by running the following command on each system:

Enable-PSRemoting -Force

Then, you can use the Invoke-Command cmdlet to run the wallpaper change script on remote systems:

$computers = @("PC1", "PC2", "PC3")
Invoke-Command -ComputerName $computers -FilePath "C:\Path\To\Change-Wallpaper.ps1"

Set Different Wallpapers for Multiple Monitors

If you have a multi-monitor setup and want to set different wallpapers for each monitor, you can use the Display class in PowerShell. Here’s an example script:

$primaryMonitorWallpaper = "C:\Users\Public\Pictures\Sample Pictures\Primary_Wallpaper.jpg"
$secondaryMonitorWallpaper = "C:\Users\Public\Pictures\Sample Pictures\Secondary_Wallpaper.jpg"

# Function to set wallpaper
function Set-Wallpaper {
    param (
        [string]$monitor,
        [string]$wallpaperPath
    )
    Add-Type @"
    using System;
    using System.Runtime.InteropServices;
    public class User32 {
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
    }
    "@
    $SPI_SETDESKWALLPAPER = 0x0014
    $SPIF_UPDATEINIFILE = 0x01
    $SPIF_SENDCHANGE = 0x02
    User32::SystemParametersInfo($SPI_SETDESKWALLPAPER, 0, $wallpaperPath, $SPIF_UPDATEINIFILE -bor $SPIF_SENDCHANGE)
}

# Set wallpapers
Set-Wallpaper -monitor "Primary" -wallpaperPath $primaryMonitorWallpaper
Set-Wallpaper -monitor "Secondary" -wallpaperPath $secondaryMonitorWallpaper

This script sets different wallpapers for primary and secondary monitors. Customize the paths to your wallpaper images accordingly.

Check out Create Local Users in Windows Using PowerShell

Troubleshooting Common Issues

Now, let me show you some common issues you might face while running the PowerShell script.

Execution Policy Error

If you encounter an error related to the execution policy, ensure that you have set the execution policy correctly using the Set-ExecutionPolicy cmdlet. You can also bypass the policy for a single script execution by running:

powershell -ExecutionPolicy Bypass -File .\Change-Wallpaper.ps1

File Path Issues

Ensure that the file path to your wallpaper image is correct. Use absolute paths to avoid any ambiguity. You can verify the path by navigating to it in File Explorer.

Permissions

Run PowerShell with administrative privileges to avoid permission-related issues. Right-click the PowerShell icon and select “Run as administrator.”

Change Wallpaper with PowerShell

Conclusion

In this tutorial, I explained how to change the desktop wallpaper with PowerShell. Following this tutorial, you can automate the wallpaper change process, customize it for different scenarios, and troubleshoot common issues effectively.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.