ARTICLE · 1524859
分享一个每次登录电脑会自动更换桌面背景的小程序。
🪟 Windows(PowerShell + 任务计划程序)
一. 准备脚本 C:\Scripts\DailyWallpaper.ps1,
(复制以下有底色代码,粘贴到一个新建NOTE记事本文件中,设置图片文件夹路径,然后保存为C:\Scripts\DailyWallpaper.ps1 )
$Folder = "E:\screenwall" # ← 改成你的旅拍文件夹
$StateFile = "$env:USERPROFILE\AppData\Local\wallpaper_index.txt"
$Exts = @("*.jpg","*.jpeg","*.png","*.webp")
$LogFile = "$env:TEMP\wallpaper_debug.log"
# 1. 获取图片列表(按文件名排序)
$imgs = @()
foreach ($e in $Exts) {
$imgs += Get-ChildItem $Folder -Filter $e -File -ErrorAction SilentlyContinue
}
$imgs = $imgs | Sort-Object Name
# 2. 文件夹为空则退出
if ($imgs.Count -eq 0) {
Add-Content $LogFile "$(Get-Date) No images, skip"
exit 0
}
# 3. 读取索引
$idx = -1
if (Test-Path $StateFile) {
$raw = Get-Content $StateFile -ErrorAction SilentlyContinue
if ($raw -match '^-?\d+$') {
$idx = [int]$raw
}
}
# 4. 索引越界保护(删图/文件损坏)
if ($idx -lt -1 -or $idx -ge $imgs.Count) {
$idx = -1
}
# 5. 下一张
$idx = ($idx + 1) % $imgs.Count
# 6. 二次兜底
if ($null -eq $imgs[$idx]) {
$idx = 0
}
$chosen = $imgs[$idx]
# 7. 保存状态
$idx | Out-File $StateFile -NoNewline -Force
# 8. 设置壁纸(✅ 正确包裹的 C# 代码块)
Add-Type -TypeDefinition @"
using System.Runtime.InteropServices;
public class Wallpaper {
[DllImport("user32.dll", SetLastError = true)]
public static extern int SystemParametersInfo(
int uAction,
int uParam,
string lpvParam,
int fuWinIni);
}
"@
[Wallpaper]::SystemParametersInfo(20, 0, $chosen.FullName, 0x01 -bor 0x02)
# 9. 日志(用英文,规避编码问题)
Add-Content $LogFile "$(Get-Date) Set wallpaper: $($chosen.Name) (idx=$idx / $($imgs.Count))"
• Win+R → taskschd.msc
• 创建基本任务 → 触发器 → 登录时
• 操作 → 启动程序:powershell.exe
• 参数:-WindowStyle Hidden -ExecutionPolicy Bypass -File "C:\Scripts\DailyWallpaper.ps1"
• 属性 → 常规勾“使用最高权限运行”;条件里取消“只有在交流电源下才运行”(笔记本必做)
首次手动跑一次验证:
Win+R →
powershell -ExecutionPolicy Bypass -File C:\Scripts\DailyWallpaper.ps1













































































