PowerShell/docs/set-wallpaper.md

126 lines
3.4 KiB
Markdown
Raw Normal View History

2024-01-25 13:37:12 +01:00
Script: *set-wallpaper.ps1*
========================
2022-02-10 09:01:07 +01:00
This PowerShell script sets the given image file as desktop wallpaper (.JPG or .PNG supported)
2021-10-17 14:33:27 +02:00
2023-07-29 10:04:38 +02:00
Parameters
----------
2021-10-17 14:33:27 +02:00
```powershell
2023-07-29 10:15:44 +02:00
PS> ./set-wallpaper.ps1 [[-ImageFile] <String>] [[-Style] <String>] [<CommonParameters>]
2022-02-10 09:01:07 +01:00
-ImageFile <String>
Specifies the path to the image file
Required? false
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters? false
2021-10-17 14:33:27 +02:00
2022-02-10 09:01:07 +01:00
-Style <String>
Specifies either Fill, Fit, Stretch, Tile, Center, or Span (default)
Required? false
Position? 2
Default value Span
Accept pipeline input? false
Accept wildcard characters? false
2021-10-17 14:33:27 +02:00
[<CommonParameters>]
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
```
2023-07-29 10:04:38 +02:00
Example
-------
2022-02-10 09:01:07 +01:00
```powershell
PS> ./set-wallpaper C:\ocean.jpg
```
2023-07-29 10:04:38 +02:00
Notes
-----
2022-11-17 19:46:02 +01:00
Author: Markus Fleschutz | License: CC0
2022-02-10 09:01:07 +01:00
2023-07-29 10:04:38 +02:00
Related Links
-------------
2022-02-10 09:01:07 +01:00
https://github.com/fleschutz/PowerShell
2023-07-29 10:04:38 +02:00
Script Content
--------------
2022-11-17 20:05:34 +01:00
```powershell
2022-11-17 20:02:26 +01:00
<#
.SYNOPSIS
Sets the given image file as desktop wallpaper
.DESCRIPTION
This PowerShell script sets the given image file as desktop wallpaper (.JPG or .PNG supported)
.PARAMETER ImageFile
Specifies the path to the image file
.PARAMETER Style
Specifies either Fill, Fit, Stretch, Tile, Center, or Span (default)
.EXAMPLE
PS> ./set-wallpaper C:\ocean.jpg
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([string]$ImageFile = "", [string]$Style = "Span")
function SetWallPaper {
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"}
}
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);
}
"@
$SPI_SETDESKWALLPAPER = 0x0014
$UpdateIniFile = 0x01
$SendChangeEvent = 0x02
$fWinIni = $UpdateIniFile -bor $SendChangeEvent
$ret = [Params]::SystemParametersInfo($SPI_SETDESKWALLPAPER, 0, $Image, $fWinIni)
}
try {
if ($ImageFile -eq "" ) { $ImageFile = read-host "Enter path to image file" }
SetWallPaper -Image $ImageFile -Style $Style
"✔️ Done."
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}
2022-11-17 20:05:34 +01:00
```
2022-11-17 20:02:26 +01:00
2024-08-15 09:51:46 +02:00
*(generated by convert-ps2md.ps1 using the comment-based help of set-wallpaper.ps1 as of 08/15/2024 09:50:53)*