Last active
November 17, 2023 14:22
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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