Use PowerShell to Set Windows 10 Desktop Wallpaper Based On Screen Resolution

Here we provide an example of how we can use PowerShell to set Windows 10 desktop wallpaper based on screen resolution.

We essentially add supported screen resolutions and associated images to a hashtable called wallpaperImages (you need to add your own). Then we get the current screen resolution and retrieve the associated image from the hashtable.

Finally we call some inline C# to set the desktop wallpaper using SystemParametersInfo .

$setWallpaperCodeBlock = @' 
using System.Runtime.InteropServices; 
namespace Win32{     
public class Wallpaper{ 
[DllImport("user32.dll", CharSet=CharSet.Auto)] 
static extern int SystemParametersInfo(int uAction,int uParam,string lpvParam,int fuWinIni); 
public static void SetWallpaper(string imagePath){ 
SystemParametersInfo(20,0,imagePath,3); 
}
}
} 
'@
add-type $setWallpaperCodeBlock
function Get-ScreenResolution {
$screen = [System.Windows.Forms.Screen]::PrimaryScreen
$width = $screen.Bounds.Width
$height = $screen.Bounds.Height
return "$($width)x$($height)"
}
$wallpaperImages = @{
"800x600" = "C:\Path\To\Image-800x600.jpg"
"1366x768" = "C:\Path\To\Image-1366x768.jpg"
"Default" = "C:\Path\To\Image.jpg"
}
#get screen resolution
$resolution = Get-ScreenResolution
if ($wallpaperImages.ContainsKey($resolution)) {
$wallpaper = $wallpaperImages[$resolution]
if (Test-path($wallpaper)) {
[Win32.Wallpaper]::SetWallpaper($wallpaper)
}
}
else {
$wallpaper = $wallpaperImages["Default"]
if (Test-path($wallpaper)) {
[Win32.Wallpaper]::SetWallpaper($wallpaperImages["Default"])
}
}