PowerShell/Scripts/set-wallpaper.ps1

73 lines
2.1 KiB
PowerShell
Raw Normal View History

2021-09-27 10:38:12 +02:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
2021-10-04 21:29:23 +02:00
Sets the given image file as desktop wallpaper
2021-07-13 21:10:02 +02:00
.DESCRIPTION
2022-01-30 10:49:30 +01:00
This PowerShell script sets the given image file as desktop wallpaper (.JPG or .PNG supported)
2021-10-16 16:50:10 +02:00
.PARAMETER ImageFile
Specifies the path to the image file
.PARAMETER Style
2021-12-11 16:50:35 +01:00
Specifies either Fill, Fit, Stretch, Tile, Center, or Span (default)
2021-07-13 21:10:02 +02:00
.EXAMPLE
2021-09-24 17:19:49 +02:00
PS> ./set-wallpaper C:\ocean.jpg
2021-07-13 21:10:02 +02:00
.LINK
https://github.com/fleschutz/PowerShell
2022-01-30 10:49:30 +01:00
.NOTES
Author: Markus Fleschutz / License: CC0
2021-01-27 16:21:34 +01:00
#>
2021-12-11 16:50:35 +01:00
param([string]$ImageFile = "", [string]$Style = "Span")
2021-01-27 16:21:34 +01:00
function SetWallPaper {
2021-02-07 15:01:08 +01:00
param([string]$Image, [ValidateSet('Fill', 'Fit', 'Stretch', 'Tile', 'Center', 'Span')][string]$Style)
$WallpaperStyle = switch($Style) {
"Fill" {"10"}
"Fit" {"6"}
"Stretch" {"2"}
"Tile" {"0"}
"Center" {"0"}
"Span" {"22"}
}
2021-01-27 16:21:34 +01:00
2021-02-07 15:01:08 +01:00
if ($Style -eq "Tile") {
New-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name WallpaperStyle -PropertyType String -Value $WallpaperStyle -Force
New-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name TileWallpaper -PropertyType String -Value 1 -Force
} else {
New-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name WallpaperStyle -PropertyType String -Value $WallpaperStyle -Force
New-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name TileWallpaper -PropertyType String -Value 0 -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);
}
2021-12-11 16:49:04 +01:00
"@
2021-01-27 16:21:34 +01:00
2021-02-07 15:01:08 +01:00
$SPI_SETDESKWALLPAPER = 0x0014
$UpdateIniFile = 0x01
$SendChangeEvent = 0x02
2021-01-27 16:21:34 +01:00
2021-02-07 15:01:08 +01:00
$fWinIni = $UpdateIniFile -bor $SendChangeEvent
2021-01-27 16:21:34 +01:00
2021-02-07 15:01:08 +01:00
$ret = [Params]::SystemParametersInfo($SPI_SETDESKWALLPAPER, 0, $Image, $fWinIni)
2021-01-27 16:21:34 +01:00
}
try {
2021-08-24 20:46:03 +02:00
if ($ImageFile -eq "" ) { $ImageFile = read-host "Enter path to image file" }
2021-01-27 16:21:34 +01:00
SetWallPaper -Image $ImageFile -Style $Style
2021-08-24 20:46:03 +02:00
"✔️ Done."
2021-09-27 10:09:45 +02:00
exit 0 # success
2021-01-27 16:21:34 +01:00
} catch {
2021-09-16 20:19:10 +02:00
"⚠️ Error: $($Error[0]) ($($MyInvocation.MyCommand.Name):$($_.InvocationInfo.ScriptLineNumber))"
2021-01-27 16:21:34 +01:00
exit 1
}