Skip to content

Instantly share code, notes, and snippets.

@rubenhorn
Created July 6, 2021 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rubenhorn/d3574b511e2918e91f336a1d272d421e to your computer and use it in GitHub Desktop.
Save rubenhorn/d3574b511e2918e91f336a1d272d421e to your computer and use it in GitHub Desktop.
Run from autostart folder using .bat-script with: start /min powershell -File path\to\random-wallpaper.ps1
#====================
# Settings
#====================
$superSampling = 2
$urlTemplate = "https://picsum.photos/{width}/{height}?grayscale"
#====================
# Await connection
#====================
$null = $urlTemplate -match "^https?://([^/]*)/"
while(-Not (Test-Connection -Count 1 -ComputerName $Matches.1 -Quiet)) {
Sleep -Seconds 60
}
#====================
# Update wallpaper
#====================
$videoModeDescription = (Get-WmiObject -Class Win32_VideoController).VideoModeDescription -split " x "
$width = ($videoModeDescription[0] -as [int]) * $superSampling
$height = ($videoModeDescription[1] -as [int]) * $superSampling
$url = $urlTemplate.Replace("{width}", $width).Replace("{height}", $height)
$filename = [Environment]::GetFolderPath("MyPictures") + "\wallpaper.png"
Invoke-WebRequest $url -OutFile $filename
Set-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name WallPaper -Value $filename
# Force wallpaper update
Sleep -Seconds 5
RUNDLL32.EXE USER32.DLL,UpdatePerUserSystemParameters 1, True
RUNDLL32.EXE USER32.DLL,UpdatePerUserSystemParameters 1, True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment