Created
December 14, 2021 14:08
-
-
Save vexx32/e8128cbc19881fb965a286cf0bc613fc to your computer and use it in GitHub Desktop.
PowerShell function that changes Windows wallpaper with p/invoke
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Set-Wallpaper { | |
<# | |
.SYNOPSIS | |
Applies a specified wallpaper to the current user's desktop | |
.EXAMPLE | |
Set-WallPaper -Image "C:\Wallpaper\Default.jpg" | |
#> | |
[CmdletBinding()] | |
param( | |
# Provide the path to the image | |
[Parameter(Mandatory)] | |
[Alias('Image', 'ImagePath')] | |
[string] | |
$Path | |
) | |
Add-Type -TypeDefinition @' | |
using System; | |
using System.Runtime.InteropServices; | |
public class SystemParameters | |
{ | |
[DllImport("User32.dll",CharSet=CharSet.Unicode)] | |
public static extern int SystemParametersInfo( | |
Int32 uAction, | |
Int32 uParam, | |
String lpvParam, | |
Int32 fuWinIni); | |
} | |
'@ | |
# 0x0014 = SPI_SETDESKWALLPAPER | |
$setWallpaperAction = 0x0014 | |
$UpdateIniFile = 0x01 | |
$SendChangeEvent = 0x02 | |
$options = $UpdateIniFile -bor $SendChangeEvent | |
$finalPath = Resolve-Path -Path $Path | |
$null = [SystemParameters]::SystemParametersInfo($setWallpaperAction, 0, $finalPath, $options) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment