Skip to content

Instantly share code, notes, and snippets.

@lelegard
Created April 27, 2022 09:03
Show Gist options
  • Save lelegard/5f5e69b1bac2edd42c2e20084525fe4a to your computer and use it in GitHub Desktop.
Save lelegard/5f5e69b1bac2edd42c2e20084525fe4a to your computer and use it in GitHub Desktop.
Set Windows 10 wallpaper from PowerShell
function Set-WallPaper {
<#
.SYNOPSIS
Applies a specified wallpaper to the current user's desktop
Reference: https://www.joseespitia.com/2017/09/15/set-wallpaper-powershell-function/
.PARAMETER Image
Provide the exact path to the image
.PARAMETER Style
Provide wallpaper style, one of Fill, Fit, Stretch, Tile, Center, Span
#>
param (
[parameter(Mandatory=$True)][string]$Image,
[parameter(Mandatory=$False)][ValidateSet('Fill', 'Fit', 'Stretch', 'Tile', 'Center', 'Span')][string]$Style
)
$WallpaperTile = if ($Style -eq "Tile") {1} else {0}
$WallpaperStyle = switch ($Style) {
"Fill" {"10"}
"Fit" {"6"}
"Stretch" {"2"}
"Tile" {"0"}
"Center" {"0"}
"Span" {"22"}
}
[void](New-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name WallpaperStyle -PropertyType String -Value $WallpaperStyle -Force)
[void](New-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name TileWallpaper -PropertyType String -Value $WallpaperTile -Force)
Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
public class Params
{
[DllImport("User32.dll",CharSet=CharSet.Unicode)]
public static extern int SystemParametersInfo(Int32 uAction, Int32 uParam, String lpvParam, Int32 fuWinIni);
}
"@
$SPI_SETDESKWALLPAPER = 0x0014
$UpdateIniFile = 0x01
$SendChangeEvent = 0x02
$fWinIni = $UpdateIniFile -bor $SendChangeEvent
[void][Params]::SystemParametersInfo($SPI_SETDESKWALLPAPER, 0, $Image, $fWinIni)
}
# Sample use (scuba diver image on Windows 10):
Set-WallPaper -Image "C:\Windows\Web\Wallpaper\Theme1\img2.jpg" -Style Fill
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment