Skip to content

Instantly share code, notes, and snippets.

@changbowen
Last active November 17, 2023 14:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save changbowen/817e054f54cac3d9160b2b49af3946fb to your computer and use it in GitHub Desktop.
Save changbowen/817e054f54cac3d9160b2b49af3946fb to your computer and use it in GitHub Desktop.
PowerShell script to be used with bginfo.exe that cycles wallpaper based on date
param(
# The folder containing the images
[Parameter()][string]$imgPath = $env:imgPath,
[Parameter()][string]$bgiPath = $env:bgiPath
)
if ([string]::IsNullOrEmpty($imgPath) -or [string]::IsNullOrEmpty($bgiPath)) {
throw "You need to set imgPath and bgiPath environment variables or pass them in."
}
#region function definitions
function Set-WallPaper($value) {
Set-ItemProperty -Path 'HKCU:\Control Panel\Desktop\' -Name WallPaper -Value $value
Set-ItemProperty -Path 'HKCU:\Control Panel\Desktop\' -Name WallpaperStyle -Value 10
Set-ItemProperty -Path 'HKCU:\Control Panel\Desktop\' -Name TileWallpaper -Value 0
RUNDLL32.EXE USER32.DLL,UpdatePerUserSystemParameters 1, True
}
#endregion
# get image list
$imgList = Get-ChildItem $imgPath |
Where-Object {('.jpg','.bmp','.png').Contains($_.Extension)} |
Sort-Object { [regex]::Replace($_, '\d+', { $args[0].Value.PadLeft(20) }) } # natual sort
if ($imgList.Length -eq 0) { throw ('No image found under ' + $imgPath) }
# select a wallpaper based on date
[System.IO.FileSystemInfo]$img = $null
$imgIdx = 0
$now = Get-Date
if ($imgList.Length -gt 12) {
# more than 12 images found (distributed over a year)
$imgIdx = ($now.DayOfYear - 1) % $imgList.Length
}
else {
# less than 12 images (one per month)
$imgIdx = ($now.Month - 1) % $imgList.Length
}
$img = $imgList[$imgIdx]
# apply configuration
Set-WallPaper $img.FullName
Start-Process -FilePath 'bginfo.exe' -ArgumentList 'config.bgi /timer:00' -WorkingDirectory $bgiPath
Write-Host ('Wallpaper set to ' + $img.FullName)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment