You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows锁屏通过脚本管理时图片更新后无法刷新的问题咨询

Windows锁屏通过脚本管理时图片更新后无法刷新的问题咨询

问题描述

我已经通过修改注册表HKLM:\Software\Policies\Microsoft\Windows\Personalization成功在全公司推送了统一锁屏,脚本代码如下:

$RegistryPathPersonalization = 'HKLM:\Software\Policies\Microsoft\Windows\Personalization'

If (-NOT (Test-Path $RegistryPathPersonalization)) {
    New-Item -Path $RegistryPathPersonalization -Force
}

New-ItemProperty -Path $RegistryPathPersonalization -Name 'LockScreenImage' -Value 'C:\Windows\Web\CSImages\LockScreen.jpg' -PropertyType 'String'
New-ItemProperty -Path $RegistryPathPersonalization -Name 'LockScreenOverlaysDisabled' -Value '1' -PropertyType 'DWORD'
New-ItemProperty -Path $RegistryPathPersonalization -Name 'NoChangingLockScreen' -Value '1' -PropertyType 'DWORD'
New-ItemProperty -Path $RegistryPathPersonalization -Name 'NoLockScreenSlideshow' -Value '1' -PropertyType 'DWORD'

但后续替换同路径的LockScreen.jpg后,锁屏依然显示旧图片,只有在「设置-个性化-锁屏」里能看到新图。我猜测是系统缓存了旧图片,想知道怎么通过脚本更好地管理锁屏更新?


解决方案

你猜的没错,Windows会把锁屏图片缓存到用户本地缓存目录和系统级缓存中,直接替换原文件后系统还是会优先读取缓存。下面是几个可以整合到脚本里的解决步骤:

1. 清理用户级锁屏缓存

Windows把个性化相关的缓存存在每个用户的AppData目录里,我们可以删除这些缓存文件:

# 定义缓存路径(针对当前用户)
$cachePath = "$env:LOCALAPPDATA\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\Assets"

# 停止ContentDeliveryManager服务(避免文件被占用)
Stop-Service -Name ContentDeliveryManager -Force -ErrorAction SilentlyContinue

# 删除缓存文件夹内的所有文件
Remove-Item -Path "$cachePath\*" -Force -ErrorAction SilentlyContinue

# 重启服务
Start-Service -Name ContentDeliveryManager -ErrorAction SilentlyContinue

如果需要批量处理所有用户,可以遍历C:\Users下的用户目录,替换$env:LOCALAPPDATA为具体的用户路径即可(注意需要管理员权限)。

2. 强制刷新系统主题设置

清理缓存后,用命令强制系统刷新个性化设置,确保新图片生效:

# 刷新系统参数,应用新的锁屏设置
rundll32.exe user32.dll,UpdatePerUserSystemParameters 1, $true

3. 可选:重启Explorer进程(极端情况)

如果上面两步都没生效,可以尝试重启资源管理器,确保系统加载新的缓存:

Stop-Process -Name explorer -Force
Start-Process explorer

整合后的完整脚本示例

把这些步骤加到你原有的脚本末尾,替换图片后自动清理缓存并刷新:

# 原有注册表设置部分
$RegistryPathPersonalization = 'HKLM:\Software\Policies\Microsoft\Windows\Personalization'
$lockScreenImagePath = 'C:\Windows\Web\CSImages\LockScreen.jpg'

If (-NOT (Test-Path $RegistryPathPersonalization)) {
    New-Item -Path $RegistryPathPersonalization -Force
}

New-ItemProperty -Path $RegistryPathPersonalization -Name 'LockScreenImage' -Value $lockScreenImagePath -PropertyType 'String' -Force
New-ItemProperty -Path $RegistryPathPersonalization -Name 'LockScreenOverlaysDisabled' -Value '1' -PropertyType 'DWORD' -Force
New-ItemProperty -Path $RegistryPathPersonalization -Name 'NoChangingLockScreen' -Value '1' -PropertyType 'DWORD' -Force
New-ItemProperty -Path $RegistryPathPersonalization -Name 'NoLockScreenSlideshow' -Value '1' -PropertyType 'DWORD' -Force

# 替换新图片到目标路径(这里假设你已经把新图片准备好,比如从共享目录复制)
# Copy-Item -Path "\\company-share\LockScreen-New.jpg" -Destination $lockScreenImagePath -Force

# 清理缓存+刷新设置
$cachePath = "$env:LOCALAPPDATA\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\Assets"
Stop-Service -Name ContentDeliveryManager -Force -ErrorAction SilentlyContinue
Remove-Item -Path "$cachePath\*" -Force -ErrorAction SilentlyContinue
Start-Service -Name ContentDeliveryManager -ErrorAction SilentlyContinue
rundll32.exe user32.dll,UpdatePerUserSystemParameters 1, $true

注意事项

  • 脚本需要以管理员权限运行,否则无法修改HKLM注册表和系统服务。
  • 如果是域环境批量推送,可以把脚本打包成组策略对象(GPO)的登录脚本,确保每个用户登录时都会清理缓存并应用最新设置。
  • 替换图片时要确保文件名和路径和注册表中设置的完全一致,避免系统找不到新文件。

备注:内容来源于stack exchange,提问作者Nana

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.21 16:13:07