PowerShell/Scripts/set-wallpaper.ps1

63 lines
1.9 KiB
PowerShell
Raw Normal View History

2021-01-27 16:21:34 +01:00
<#
2021-04-07 15:17:49 +02:00
.SYNTAX set-wallpaper.ps1 [<image-file>] [<style>]
2021-03-22 20:10:18 +01:00
.DESCRIPTION sets the given image file as wallpaper
(style is either Fill, Fit, Stretch, Tile, Center, or Span)
.LINK https://github.com/fleschutz/PowerShell
.NOTES Author: Markus Fleschutz / License: CC0
2021-01-27 16:21:34 +01:00
#>
2021-02-18 20:17:55 +01:00
param($ImageFile = "", $Style = "Fit")
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-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 {
if ($ImageFile -eq "" ) {
$ImageFile = read-host "Enter path to image file"
}
SetWallPaper -Image $ImageFile -Style $Style
exit 0
} catch {
2021-05-02 21:30:48 +02:00
write-error "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2021-01-27 16:21:34 +01:00
exit 1
}