自动清理右键菜单的脚本怎么写

wen 实用脚本 2

本文目录导读:

自动清理右键菜单的脚本怎么写

  1. 基础版本(自动清理常见冗余项)
  2. 高级版本(交互式清理,支持自定义)
  3. 安全清理版(备份 + 还原功能)
  4. 使用说明
  5. 运行命令

基础版本(自动清理常见冗余项)

# 右键菜单清理脚本
# 以管理员身份运行
Write-Host "===== Windows右键菜单清理工具 =====" -ForegroundColor Cyan
Write-Host "正在清理常见的无用右键菜单项..." -ForegroundColor Yellow
# 1. 清理特定应用残留的右键菜单
$keysToDelete = @(
    # 文件关联菜单
    "HKCU:\Software\Classes\*\shell\TortoiseSVN",
    "HKCU:\Software\Classes\*\shell\TortoiseGit",
    "HKCU:\Software\Classes\*\shell\BeyondCompare",
    "HKCU:\Software\Classes\*\shell\WinRAR",          # 如已卸载
    "HKCU:\Software\Classes\*\shell\7-Zip",           # 如已卸载
    # 文件夹菜单
    "HKCU:\Software\Classes\Folder\shell\TortoiseSVN",
    "HKCU:\Software\Classes\Folder\shell\TortoiseGit",
    "HKCU:\Software\Classes\Folder\shell\BeyondCompare",
    # 驱动器菜单
    "HKCU:\Software\Classes\Drive\shell\TortoiseSVN"
)
foreach ($key in $keysToDelete) {
    if (Test-Path $key) {
        try {
            Remove-Item -Path $key -Recurse -Force -ErrorAction SilentlyContinue
            Write-Host "已删除: $key" -ForegroundColor Green
        } catch {
            Write-Host "删除失败: $key - $_" -ForegroundColor Red
        }
    }
}
# 2. 清理常见的Shell扩展类菜单
$shellExtensions = @(
    "HKCU:\Software\Classes\*\shellex\ContextMenuHandlers\WinRAR",
    "HKCU:\Software\Classes\*\shellex\ContextMenuHandlers\7-Zip",
    "HKCU:\Software\Classes\*\shellex\ContextMenuHandlers\DropboxExt",
    "HKCU:\Software\Classes\*\shellex\ContextMenuHandlers\OneDrive",
    "HKCU:\Software\Classes\Folder\shellex\ContextMenuHandlers\WinRAR",
    "HKCU:\Software\Classes\Folder\shellex\ContextMenuHandlers\7-Zip"
)
foreach ($key in $shellExtensions) {
    if (Test-Path $key) {
        try {
            Remove-Item -Path $key -Recurse -Force -ErrorAction SilentlyContinue
            Write-Host "已删除Shell扩展: $key" -ForegroundColor Green
        } catch {
            Write-Host "删除失败: $key - $_" -ForegroundColor Red
        }
    }
}
# 3. 清理“发送到”菜单中的无用项
$sendToPath = "$env:APPDATA\Microsoft\Windows\SendTo"
$itemsToRemove = @(
    "蓝牙设备.lnk",
    "传真收件人.lnk",
    "邮件收件人.lnk"
)
foreach ($item in $itemsToRemove) {
    $fullPath = Join-Path $sendToPath $item
    if (Test-Path $fullPath) {
        try {
            Remove-Item -Path $fullPath -Force -ErrorAction SilentlyContinue
            Write-Host "已删除发送到菜单: $item" -ForegroundColor Green
        } catch {
            Write-Host "删除失败: $item - $_" -ForegroundColor Red
        }
    }
}
Write-Host "`n清理完成!请重启资源管理器或重启电脑使更改生效。" -ForegroundColor Cyan
Write-Host "按任意键退出..." -ForegroundColor Gray
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

高级版本(交互式清理,支持自定义)

# 交互式右键菜单清理脚本
# 以管理员身份运行
# 检测管理员权限
if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
    Write-Host "请以管理员身份运行此脚本!" -ForegroundColor Red
    Start-Sleep -Seconds 2
    exit
}
Write-Host "===== 交互式右键菜单清理工具 =====`n" -ForegroundColor Cyan
# 获取所有菜单项并供用户选择
function Get-ContextMenuItems {
    param([string]$Scope = "*")
    $basePaths = @(
        "HKCU:\Software\Classes\$Scope\shell",
        "HKCU:\Software\Classes\$Scope\shellex\ContextMenuHandlers",
        "HKLM:\Software\Classes\$Scope\shell",
        "HKLM:\Software\Classes\$Scope\shellex\ContextMenuHandlers"
    )
    $items = @()
    foreach ($basePath in $basePaths) {
        if (Test-Path $basePath) {
            try {
                $subItems = Get-ChildItem -Path $basePath -ErrorAction SilentlyContinue
                foreach ($item in $subItems) {
                    $items += [PSCustomObject]@{
                        Path = $item.PSPath
                        Name = $item.PSChildName
                        RegistryPath = $basePath
                    }
                }
            } catch {}
        }
    }
    return $items
}
# 1. 文件右键菜单
Write-Host "当前文件右键菜单项 (HKCU):" -ForegroundColor Yellow
$fileMenuItems = Get-ContextMenuItems -Scope "*"
$counter = 1
$menuList = @()
foreach ($item in $fileMenuItems) {
    Write-Host "[$counter] $($item.Name) - $($item.RegistryPath)" -ForegroundColor White
    $menuList += $item
    $counter++
}
# 2. 文件夹右键菜单
Write-Host "`n当前文件夹右键菜单项:" -ForegroundColor Yellow
$folderMenuItems = Get-ContextMenuItems -Scope "Folder"
$folderStart = $counter
foreach ($item in $folderMenuItems) {
    Write-Host "[$counter] $($item.Name) - $($item.RegistryPath)" -ForegroundColor White
    $menuList += $item
    $counter++
}
# 3. 用户选择删除
Write-Host "`n请输入要删除的菜单项编号(多个用逗号分隔,输入all全选,输入q退出): " -ForegroundColor Cyan
$input = Read-Host
if ($input -eq 'q') {
    exit
}
$indicesToDelete = @()
if ($input -eq 'all') {
    $indicesToDelete = 1..($menuList.Count - 1)
} else {
    $input -split ',' | ForEach-Object {
        $trimmed = $_.Trim()
        if ($trimmed -match '^\d+$' -and [int]$trimmed -le $menuList.Count) {
            $indicesToDelete += [int]$trimmed - 1
        }
    }
}
# 执行删除
$deletedCount = 0
foreach ($index in $indicesToDelete) {
    $item = $menuList[$index]
    if ($item -and (Test-Path $item.Path)) {
        try {
            Remove-Item -Path $item.Path -Recurse -Force -ErrorAction SilentlyContinue
            Write-Host "已删除: $($item.Name)" -ForegroundColor Green
            $deletedCount++
        } catch {
            Write-Host "删除失败: $($item.Name) - $_" -ForegroundColor Red
        }
    }
}
Write-Host "`n共删除 $deletedCount 个菜单项!" -ForegroundColor Cyan
Write-Host "是否重启资源管理器?(y/n): " -ForegroundColor Yellow
$restartExplorer = Read-Host
if ($restartExplorer -eq 'y') {
    Stop-Process -Name explorer -Force -ErrorAction SilentlyContinue
    Start-Sleep -Seconds 2
    Start-Process explorer
    Write-Host "资源管理器已重启!" -ForegroundColor Green
}
Write-Host "按任意键退出..." -ForegroundColor Gray
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

安全清理版(备份 + 还原功能)

# 安全清理脚本(带备份功能)
# 以管理员身份运行
$backupDir = "$env:USERPROFILE\Desktop\ContextMenuBackup_$(Get-Date -Format 'yyyyMMdd_HHmmss')"
New-Item -ItemType Directory -Path $backupDir -Force | Out-Null
Write-Host "备份目录: $backupDir" -ForegroundColor Cyan
# 备份并删除指定的菜单项
$itemsToClean = @(
    @{Path = "HKCU:\Software\Classes\*\shell\TortoiseSVN"; Name = "TortoiseSVN"},
    @{Path = "HKCU:\Software\Classes\Folder\shell\TortoiseGit"; Name = "TortoiseGit"},
    @{Path = "HKCU:\Software\Classes\*\shellex\ContextMenuHandlers\WinRAR"; Name = "WinRAR 32位"}
)
foreach ($item in $itemsToClean) {
    if (Test-Path $item.Path) {
        # 备份
        $backupPath = Join-Path $backupDir ($item.Name + ".reg")
        Start-Process -FilePath "reg.exe" -ArgumentList "export `"$($item.Path)`" `"$backupPath`"" -Wait -WindowStyle Hidden
        # 删除
        Remove-Item -Path $item.Path -Recurse -Force -ErrorAction SilentlyContinue
        Write-Host "已备份并删除: $($item.Name)" -ForegroundColor Green
    }
}
Write-Host "`n所有删除操作均已备份到:$backupDir" -ForegroundColor Yellow
Write-Host "如需恢复,双击对应的.reg文件即可。" -ForegroundColor Cyan

使用说明

  1. 保存脚本:将上述代码保存为 CleanContextMenu.ps1

  2. 以管理员身份运行

    • 右键点击脚本文件 → 选择“使用 PowerShell 运行”
    • 或在 PowerShell 中执行:Start-Process powershell -Verb RunAs
  3. 注意事项

    • 清理前建议创建系统还原点
    • 某些菜单项可能是系统必需的,删除前请确认
    • 如果误删,可以使用备份文件恢复
    • 部分菜单项重启资源管理器后才能生效

运行命令

直接在 PowerShell 中执行:

# 先解除执行策略限制(如需要)
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
# 运行脚本
.\CleanContextMenu.ps1

这个脚本可以帮助您快速清理右键菜单中的无用项,提升操作效率,如有特殊需求,可以根据实际情况调整要删除的注册表路径。

抱歉,评论功能暂时关闭!