切换墙纸PowerShell

时间:2018-12-10 09:54:39

标签: powershell

我试图在Win10上的PowerShell ISE中创建一个脚本,该脚本可切换我的桌面背景。

不起作用。我的脚本怎么了? $newWallpaper似乎具有正确的值,因此最终Set-Wallpaper函数似乎无法正常工作。

还是我需要更改任何其他设置才能使其正常工作?

#To make powershell scripts work in general type 
#"set-executionpolicy remotesigned" 
#into the PS-Console firstly

#This script changes the background picture
Function Set-WallPaper($Value) {
    Set-ItemProperty -Path 'HKCU:\Control Panel\Desktop' -Name wallpaper -Value $value
    rundll32.exe user32.dll, UpdatePerUserSystemParameters
}

$testWallpaper = "C:\Users\test\Pictures\Backgrounds\test_wallpaper.jpg"
$testWallpaperPlaceholder = "*test_wallpaper.jpg*"
$otherWallpaper = "C:\Users\test\Pictures\Backgrounds\Dark_Canyon.jpg"

#Get-ItemProperty -path 'HKCU:\Control Panel\Desktop' | Select -Property WallPaper
$currentWallpaper = Get-ItemProperty -Path 'HKCU:\Control Panel\Desktop'
$currentWallpaper = $currentWallpaper.WallPaper

$isTest = $currentWallpaper -like $testWallpaperPlaceholder
$newWallpaper = if ($isTest) {$otherWallpaper} else {$testWallpaper}

Set-WallPaper -Value $newWallpaper.Trim('"')

0 个答案:

没有答案