从快捷方式更改桌面的 Powershell 脚本

标签 powershell

任何关于为什么在 PS 中运行时有效的想法和建议,但在从定义为的快捷方式运行时无效:

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -File "C:\Users\bin\ChangeDesktop.ps1"

ChangeDesktop.ps1 的内容:
set-itemproperty -path "HKCU:Control Panel\Desktop" -name WallPaper -value ""
rundll32.exe user32.dll, UpdatePerUserSystemParameters

如果我在 PS“命令提示符”环境中,桌面背景会自动删除并刷新,除此之外,我必须手动刷新桌面才能使更改生效。

系统是 Windows Server 2008 R2 - 全新安装。脚本执行策略设置为 RemoteSigned,我没有看到任何 PS 错误。从桌面快捷方式运行时,我只是看不到桌面自动刷新。

抓头

最佳答案

这个脚本可以创造奇迹。对于域部署,我们不希望它在每次用户登录时不断更改背景。

我进行了以下更改,以便它检查背景是否存在于计算机上所需位置,如果确实存在则退出,如果不继续文件复制并设置背景。

它首先映射隐藏共享,将文件复制到所需目录,设置壁纸,然后断开隐藏共享。如果您的公司已使用“X”,请插入另一个驱动器号。 :D

$strFileName="C:\Users\Public\Pictures\background.jpg"
If (Test-Path $strFileName){
  # // File exists
  Exit-PSSession
}Else{
  # // File does not exist 
New-PSDrive -Name X -PSProvider Filesystem -Root   \\hiddenfileshare\wallpapers
Copy-Item X:\background.jpg C:\Users\Public\Pictures
[Wallpaper.Setter]::SetWallpaper( 'C:\Users\Public\Pictures\background.jpg',       0 )
Remove-PSDrive X
}

关于从快捷方式更改桌面的 Powershell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9440135/

相关文章:

powershell - 通过 Powershell 对 AD 对象的属性进行排序

PowerShell脚本模拟 "Enter"按下

python - Powershell执行python : redirect print statements only?

Powershell 压缩文件夹和文件,然后删除旧文件

powershell - 如何拥有父脚本但保持与 Powershell 脚本的关注点分离?

Python Popen 无法在 Windows PowerShell 中使用正确的编码

powershell - 无法使用 powershell 启动 azure 数据库的无人值守导入

powershell - 列出Active Directory组层次结构

arrays - Powershell替换数组错误中的值

windows - 使用 New-PsDrive 时 Powershell 作业卡住