Added set-wallpaper.ps1

This commit is contained in:
Markus Fleschutz 2021-01-27 16:21:34 +01:00
parent dcc52122c4
commit 2e12acbf92
3 changed files with 97 additions and 0 deletions

View File

@ -65,6 +65,7 @@ send-email.ps1, sends an email message
send-tcp.ps1, sends a TCP message to the given IP address and port send-tcp.ps1, sends a TCP message to the given IP address and port
send-udp.ps1, sends a UDP datagram message to the given IP address and port send-udp.ps1, sends a UDP datagram message to the given IP address and port
set-timer.ps1, sets a timer for a countdown set-timer.ps1, sets a timer for a countdown
set-wallpaper.ps1, sets the given image as wallpaper
SHA1.ps1, prints the SHA1 checksum of the given file SHA1.ps1, prints the SHA1 checksum of the given file
SHA256.ps1, prints the SHA256 checksum of the given file SHA256.ps1, prints the SHA256 checksum of the given file
simulate-matrix.ps1, simulates the Matrix (fun) simulate-matrix.ps1, simulates the Matrix (fun)

1 Filename Description
65 send-tcp.ps1 sends a TCP message to the given IP address and port
66 send-udp.ps1 sends a UDP datagram message to the given IP address and port
67 set-timer.ps1 sets a timer for a countdown
68 set-wallpaper.ps1 sets the given image as wallpaper
69 SHA1.ps1 prints the SHA1 checksum of the given file
70 SHA256.ps1 prints the SHA256 checksum of the given file
71 simulate-matrix.ps1 simulates the Matrix (fun)

View File

@ -73,6 +73,7 @@ The following PowerShell scripts can be found in the [Scripts/](Scripts/) subfol
* [send-tcp.ps1](Scripts/send-udp.ps1) - sends a TCP message to the given IP address and port * [send-tcp.ps1](Scripts/send-udp.ps1) - sends a TCP message to the given IP address and port
* [send-udp.ps1](Scripts/send-udp.ps1) - sends a UDP datagram message to the given IP address and port * [send-udp.ps1](Scripts/send-udp.ps1) - sends a UDP datagram message to the given IP address and port
* [set-timer.ps1](Scripts/set-timer.ps1) - sets a timer for a countdown * [set-timer.ps1](Scripts/set-timer.ps1) - sets a timer for a countdown
* [set-wallpaper.ps1](Scripts/set-wallpaper.ps1) - sets the given image as wallpaper
* [SHA1.ps1](Scripts/SHA1.ps1) - prints the SHA1 checksum of the given file * [SHA1.ps1](Scripts/SHA1.ps1) - prints the SHA1 checksum of the given file
* [SHA256.ps1](Scripts/SHA256.ps1) - prints the SHA256 checksum of the given file * [SHA256.ps1](Scripts/SHA256.ps1) - prints the SHA256 checksum of the given file
* [simulate-matrix.ps1](Scripts/simulate-matrix.ps1) - simulates the Matrix (fun) * [simulate-matrix.ps1](Scripts/simulate-matrix.ps1) - simulates the Matrix (fun)

95
Scripts/set-wallpaper.ps1 Normal file
View File

@ -0,0 +1,95 @@
#!/snap/bin/powershell
<#
.SYNTAX ./set-wallpaper.ps1 [<image-file>] [<style>]
.DESCRIPTION sets the given image file as wallpaper
.LINK https://github.com/fleschutz/PowerShell
.NOTES Author: Markus Fleschutz / License: CC0
#>
param([string]$ImageFile = "", [string]$Style = "Fit")
function SetWallPaper {
<#
.SYNOPSIS
Applies a specified wallpaper to the current user's desktop
.PARAMETER Image
Provide the exact path to the image
.PARAMETER Style
Provide wallpaper style (Example: Fill, Fit, Stretch, Tile, Center, or Span)
.EXAMPLE
Set-WallPaper -Image "C:\Wallpaper\Default.jpg"
Set-WallPaper -Image "C:\Wallpaper\Background.jpg" -Style Fit
#>
param (
[parameter(Mandatory=$True)]
# Provide path to image
[string]$Image,
# Provide wallpaper style that you would like applied
[parameter(Mandatory=$False)]
[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
exit 0
} catch {
write-error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}