Windows11の壁紙を条件で切り替える方法

タスクスケジューラーとシェルスクリプトで実現可能。

Add-Type @"
using System.Runtime.InteropServices;
public class Wallpaper {
    [DllImport("user32.dll", SetLastError=true)]
    public static extern bool SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
}
"@

$wallpaper1 = "C:\WallpaperSchedule\wallpaper1.jpg"
$wallpaper2 = "C:\WallpaperSchedule\wallpaper2.jpg"

$now = Get-Date
$isThursday = ($now.DayOfWeek -eq "Thursday")
$hour = $now.Hour

if ($isThursday -and $hour -ge 16 -and $hour -lt 19) {
    $path = $wallpaper1
} else {
    $path = $wallpaper2
}

if (Test-Path $path) {
    [Wallpaper]::SystemParametersInfo(20, 0, $path, 3)
}

壁紙のファイルは

C:\WallpaperSchedule\wallpaper1.jpg

C:\WallpaperSchedule\wallpaper2.jpg

のようにおいておく。

タスクスケジューラーで、ログオン時に C:\WallpaperSchedule\set-wallpaper.ps1 を実行するようにする。

全般 タブ

  • 名前:曜日で壁紙変更
  • ユーザーがログオンしているときのみ実行する を選ぶ

トリガー タブ

  • 新規
  • タスクの開始ログオン時
  • 必要なら 特定のユーザー を自分にする
  • OK

操作 タブ

  • 新規
  • 操作プログラムの開始
  • プログラム/スクリプトpowershell.exe
  • 引数の追加-ExecutionPolicy Bypass -File "C:\WallpaperSchedule\set-wallpaper.ps1"

条件 タブ

  • (任意) AC電源でのみ開始 のチェックを外す

設定 タブ

  • 要求時にタスクを実行する にチェック

コメントする