#Requires -Version 7.0 <# .SYNOPSIS 在 Windows 资源管理器右键菜单里注册「SceneRename 重命名」。 .DESCRIPTION 只写 HKCU(当前用户),不需要管理员权限,可随时用 uninstall-explorer-menu.ps1 移除。 注册位置: HKCU\Software\Classes\Directory\shell\SceneRename (文件夹右键) HKCU\Software\Classes\Directory\Background\shell\SceneRename (文件夹空白处右键) HKCU\Software\Classes\SystemFileAssociations\.mkv\shell\SceneRename ... 同上 .mp4 / .avi(文件右键,取其所在目录) .EXAMPLE .\install-explorer-menu.ps1 #> [CmdletBinding()] param() $ErrorActionPreference = 'Stop' $runner = Join-Path $PSScriptRoot 'explorer-media-rename.ps1' if (-not (Test-Path -LiteralPath $runner)) { throw "找不到 $runner" } $alias = Join-Path $env:LOCALAPPDATA 'Microsoft\WindowsApps\pwsh.exe' if (Test-Path -LiteralPath $alias) { # 应用执行别名,跨 PowerShell 版本升级保持稳定 $pwsh = $alias } else { $pwsh = (Get-Command pwsh -ErrorAction Stop).Source } function New-MenuCommand([string]$Token) { # Token: %1 = 选中的文件夹/文件;%V = 当前目录 return '"' + $pwsh + '" -NoLogo -NoProfile -File "' + $runner + '" -Pause "' + $Token + '"' } $menuText = 'SceneRename 重命名' $targets = @( @{ Key = 'HKCU:\Software\Classes\Directory\shell\SceneRename'; Token = '%1' }, @{ Key = 'HKCU:\Software\Classes\Directory\Background\shell\SceneRename'; Token = '%V' }, @{ Key = 'HKCU:\Software\Classes\SystemFileAssociations\.mkv\shell\SceneRename'; Token = '%1' }, @{ Key = 'HKCU:\Software\Classes\SystemFileAssociations\.mp4\shell\SceneRename'; Token = '%1' }, @{ Key = 'HKCU:\Software\Classes\SystemFileAssociations\.avi\shell\SceneRename'; Token = '%1' } ) foreach ($item in $targets) { $commandKey = Join-Path $item.Key 'command' New-Item -Path $item.Key -Force | Out-Null New-Item -Path $commandKey -Force | Out-Null New-ItemProperty -Path $item.Key -Name '(default)' -Value $menuText -PropertyType String -Force | Out-Null New-ItemProperty -Path $item.Key -Name 'Icon' -Value $pwsh -PropertyType String -Force | Out-Null New-ItemProperty -Path $commandKey -Name '(default)' -Value (New-MenuCommand $item.Token) -PropertyType String -Force | Out-Null } Write-Host '✔ 已注册右键菜单(当前用户)' -ForegroundColor Green $targets | ForEach-Object { Write-Host (' - ' + $_.Key) -ForegroundColor DarkGray } Write-Host '提示:若菜单未立即出现,重启一次 explorer.exe 或重新登录。' -ForegroundColor Yellow