PowerShell 脚本:删除当前桌面壁纸文件

Windows 中将桌面壁纸设置为幻灯片播放后,遇到不喜欢的壁纸,可以通过这个脚本找到其路径并一键删除。

[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Windows.Forms.Application]::EnableVisualStyles()

$TranscodedImageCache=(Get-ItemProperty 'HKCU:\Control Panel\Desktop' TranscodedImageCache -ErrorAction Stop).TranscodedImageCache

$Path_Start_Delta = 24
$Path_End_Delta   = $TranscodedImageCache.length-1
for ($i = $Path_Start_Delta; $i -lt ($TranscodedImageCache.length); $i += 2)
{
    if ($TranscodedImageCache[($i+2)..($i+3)] -eq 0) {
        $Path_End_Delta=$i + 1;
        Break;
    }
}
$UnicodeObject=New-Object System.Text.UnicodeEncoding
$WallpaperSource=$UnicodeObject.GetString($TranscodedImageCache[$Path_Start_Delta..$Path_End_Delta]);

$result=[System.Windows.Forms.MessageBox]::Show("Wallpaper location: `r$WallpaperSource`r`rDelete it?", "Script", "YesNo", "Asterisk");
if ($result -eq "Yes")
{
    Add-Type -AssemblyName Microsoft.VisualBasic
    [Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile($WallpaperSource,'OnlyErrorDialogs','SendToRecycleBin')
}

发布者

胡中元

《中原驿站》站长

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注