再增加管理员权限
tui-config.json
{
"$remark": "在此配置你的 TUI 工具列表,修改后运行 tui.ps1 生效",
"menuTitle": "TUI Here",
"icon": "powershell.exe",
"tools": [
{ "name": "Omp", "command": "D:\\tools\\oh-my-pi\\omp-windows-x64.exe", "admin": true },
{ "name": "Deepx", "command": "D:\\tools\\deepx\\deepx.exe", "admin": false },
{ "name": "CMD", "command": "cmd.exe" , "admin": true},
{ "name": "btop", "command": "btop.exe" },
{ "name": "Terminal","command": "wt.exe" }
]
}
tui.ps1
#Requires -Version 5.0
<#
.SYNOPSIS
Interactive manager for TUI Explorer context menu.
Supports [1] Register/Update (with optional Admin UAC) and [2] Uninstall/Remove.
Fully compatible with PowerShell 5.1 (Win10/Win11 default).
Usage:
powershell -ExecutionPolicy Bypass -File "path\to\tui.ps1"
#>
$ErrorActionPreference = 'Stop'
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$configFile = Join-Path $scriptDir "tui-config.json"
$isAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
# ==================== 卸载逻辑 ====================
function Uninstall-TUIMenu {
Write-Host "`n🗑️ --- 开始卸载上下文菜单 ---" -ForegroundColor DarkGray
$targets = @(
"HKCU:\Software\Classes\Directory\Background\shell\OpenTUI",
"HKCU:\Software\Classes\Directory\shell\OpenTUI",
"HKCU:\Software\Classes\TUIHereCommands"
)
foreach ($path in $targets) {
if (Test-Path $path) {
Remove-Item -Path $path -Recurse -Force -ErrorAction SilentlyContinue
Write-Host " [已移除] $path" -ForegroundColor Yellow
}
}
if ($isAdmin) {
$legacy = @("HKCR:\Directory\Background\shell\OpenTUI", "HKCR:\Directory\shell\OpenTUI")
foreach ($path in $legacy) {
if (Test-Path $path) {
Remove-Item -Path $path -Recurse -Force -ErrorAction SilentlyContinue
Write-Host " [已移除 HKCR] $path" -ForegroundColor Yellow
}
}
}
Write-Host " ✅ 卸载完成!右键菜单已彻底移除。" -ForegroundColor Green
Read-Host "`n按 Enter 键返回主菜单"
}
# ==================== 注册逻辑 ====================
function Install-TUIMenu {
if (-not (Test-Path $configFile)) {
Write-Host "❌ [ERROR] 配置文件未找到: $configFile" -ForegroundColor Red
Read-Host "`n按 Enter 键返回主菜单"; return
}
try {
$config = Get-Content $configFile -Raw -Encoding UTF8 | ConvertFrom-Json
} catch {
Write-Host "❌ [ERROR] 配置文件解析失败: $_" -ForegroundColor Red
Read-Host "`n按 Enter 键返回主菜单"; return
}
$menuTitle = if ($config.menuTitle) { $config.menuTitle } else { "TUI Here" }
$icon = if ($config.icon) { $config.icon } else { "powershell.exe" }
$tools = $config.tools
if (-not $tools -or $tools.Count -eq 0) {
Write-Host "⚠️ [WARN] 配置文件中 tools 数组为空,已跳过注册。" -ForegroundColor Yellow
Read-Host "`n按 Enter 键返回主菜单"; return
}
Write-Host "`n🧹 --- 清理旧注册表项 ---" -ForegroundColor DarkGray
$subKeys = @("Directory\Background\shell\OpenTUI", "Directory\shell\OpenTUI")
foreach ($sk in $subKeys) {
$rp = "HKCU:\Software\Classes\$sk"
if (Test-Path $rp) { Remove-Item -Path $rp -Recurse -Force; Write-Host " 已清理 (HKCU): $sk" -ForegroundColor DarkGray }
}
if ($isAdmin) {
foreach ($sk in $subKeys) {
$rp = "HKCR:\$sk"
if (Test-Path $rp) { Remove-Item -Path $rp -Recurse -Force; Write-Host " 已清理 (HKCR): $sk" -ForegroundColor DarkGray }
}
}
Write-Host "📝 --- 创建 '$menuTitle' 上下文菜单 ---" -ForegroundColor DarkGray
foreach ($sk in $subKeys) {
$regPath = "HKCU:\Software\Classes\$sk"
New-Item -Path $regPath -Force | Out-Null
Set-ItemProperty -Path $regPath -Name "(Default)" -Value $menuTitle
Set-ItemProperty -Path $regPath -Name "MUIVerb" -Value $menuTitle
Set-ItemProperty -Path $regPath -Name "Icon" -Value $icon
Set-ItemProperty -Path $regPath -Name "ExtendedSubCommandsKey" -Value "TUIHereCommands"
Set-ItemProperty -Path $regPath -Name "Position" -Value "Bottom"
}
$shellBase = "HKCU:\Software\Classes\TUIHereCommands\shell"
if (Test-Path $shellBase) { Remove-Item -Path $shellBase -Recurse -Force -ErrorAction SilentlyContinue }
New-Item -Path $shellBase -Force | Out-Null
$validCount = 0
foreach ($tool in $tools) {
$toolName = $tool.name
$toolCmd = $tool.command
# ✅ 修复:替换 PS 7+ 的 ?. 语法,改为 PS 5.1 兼容写法
$isAdminFlag = $false
if ($null -ne $tool.admin) {
$isAdminFlag = [bool]$tool.admin
}
if (-not $toolName -or -not $toolCmd) { continue }
if ([System.IO.Path]::IsPathRooted($toolCmd) -and -not (Test-Path $toolCmd -ErrorAction SilentlyContinue)) {
Write-Host " ⚠️ 跳过无效路径: $toolCmd" -ForegroundColor Yellow; continue
}
$subPath = Join-Path $shellBase $toolName
New-Item -Path $subPath -Force | Out-Null
Set-ItemProperty -Path $subPath -Name "(Default)" -Value $toolName
$cmdPath = Join-Path $subPath "command"
New-Item -Path $cmdPath -Force | Out-Null
# 转义单引号防命令注入
$safeCmd = $toolCmd -replace "'", "''"
$innerCmd = "Set-Location -LiteralPath '%V'; & '$safeCmd'"
$escapedInner = $innerCmd -replace "'", "''"
if ($isAdminFlag) {
# UAC 提权路径
$regCommand = "powershell.exe -NoProfile -Command `"Start-Process powershell.exe -Verb RunAs -ArgumentList '-NoProfile','-NoExit','-Command','$escapedInner'`""
} else {
# 普通路径
$regCommand = "powershell.exe -NoProfile -NoExit -Command `"Set-Location -LiteralPath '%V'; & '$safeCmd'`""
}
Set-ItemProperty -Path $cmdPath -Name "(Default)" -Value $regCommand
$validCount++
}
Write-Host " ✅ 注册成功!共加载 $validCount 个工具(含提权项)。" -ForegroundColor Green
Read-Host "`n按 Enter 键返回主菜单"
}
# ==================== 交互主循环 ====================
do {
Clear-Host
Write-Host "=========================================" -ForegroundColor Cyan
Write-Host " TUI Context Menu Manager " -ForegroundColor Cyan
Write-Host "=========================================" -ForegroundColor Cyan
Write-Host "[1] 📥 注册/更新菜单 (Register/Update)" -ForegroundColor Green
Write-Host "[2] 🗑️ 卸载/移除菜单 (Uninstall/Remove)" -ForegroundColor Red
Write-Host "[Q] 🚪 退出脚本 (Quit)" -ForegroundColor Yellow
Write-Host "=========================================" -ForegroundColor Cyan
$choice = (Read-Host "请输入选项数字 (1/2/Q)").Trim().ToUpper()
switch ($choice) {
"1" { Install-TUIMenu }
"2" { Uninstall-TUIMenu }
"Q" { Write-Host "`n👋 已安全退出。" -ForegroundColor Gray; return }
default { Write-Host "⚠️ 无效选项,请重新输入!" -ForegroundColor Red; Start-Sleep -Seconds 1 }
}
} while ($true)
|