直接使用这个ps1进行清除
# p4-force-uninstall.ps1 - 强制清除 Helix Core / Perforce Server 的安装登记, 解决 "已经安装, 无法再次安装"
# 用法 (管理员 PowerShell):
# powershell -ExecutionPolicy Bypass -File .\p4-force-uninstall.ps1
# 参数: -Yes 跳过确认 | -DeleteData 同时删除 P4ROOT 等数据目录 | -IncludeClients 连 P4V/P4Admin 也清
param([switch]$Yes, [switch]$DeleteData, [switch]$IncludeClients)
function Say($m, $c = 'Cyan') { Write-Host $m -ForegroundColor $c }
$admin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()
).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $admin) { Say "请以管理员身份运行 PowerShell" Red; exit 1 }
$nameRx = 'Perforce|Helix'
$serverRx = 'Server|Versioning Engine|P4D|Core|Broker|Proxy'
$clientRx = 'P4V|Visual Client|P4Admin|P4Merge|Plugin|Swarm'
function IsTarget($n) {
if (-not $n -or $n -notmatch $nameRx) { return $false }
if ($IncludeClients) { return $true }
return ($n -match $serverRx) -and ($n -notmatch $clientRx)
}
$items = New-Object System.Collections.ArrayList # 要清理的注册表项
function Add-Item($type, $key, $name, $id, $uninstall) {
if (-not (Test-Path -LiteralPath $key)) { return }
if ($items | Where-Object { $_.Key -eq $key }) { return }
[void]$items.Add([pscustomobject]@{ Type = $type; Key = $key; Name = $name; Id = $id; Uninstall = $uninstall })
}
# ---------- 1. 卸载列表 (应用和功能里的条目) ----------
$uroots = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall',
'HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall',
'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall'
foreach ($r in $uroots) {
if (-not (Test-Path $r)) { continue }
foreach ($k in Get-ChildItem $r -ErrorAction SilentlyContinue) {
$p = Get-ItemProperty -LiteralPath $k.PSPath -ErrorAction SilentlyContinue
if (IsTarget $p.DisplayName) {
Add-Item 'Uninstall' $k.PSPath $p.DisplayName $k.PSChildName $p.UninstallString
}
}
}
# ---------- 2. Windows Installer 内部登记 (安装程序检测 "已安装" 主要看这里) ----------
$ud = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData'
if (Test-Path $ud) {
foreach ($u in Get-ChildItem $ud -ErrorAction SilentlyContinue) {
$prodRoot = "$($u.PSPath)\Products"
if (-not (Test-Path $prodRoot)) { continue }
foreach ($pk in Get-ChildItem $prodRoot -ErrorAction SilentlyContinue) {
$ip = Get-ItemProperty -LiteralPath "$($pk.PSPath)\InstallProperties" -ErrorAction SilentlyContinue
if (IsTarget $ip.DisplayName) {
$packed = $pk.PSChildName
Add-Item 'Installer.UserData' $pk.PSPath $ip.DisplayName $packed $null
Add-Item 'Installer.Product' "HKLM:\SOFTWARE\Classes\Installer\Products\$packed" $ip.DisplayName $packed $null
Add-Item 'Installer.Feature' "HKLM:\SOFTWARE\Classes\Installer\Features\$packed" $ip.DisplayName $packed $null
}
}
}
}
$cp = 'HKLM:\SOFTWARE\Classes\Installer\Products'
if (Test-Path $cp) {
foreach ($pk in Get-ChildItem $cp -ErrorAction SilentlyContinue) {
$pn = (Get-ItemProperty -LiteralPath $pk.PSPath -ErrorAction SilentlyContinue).ProductName
if (IsTarget $pn) {
Add-Item 'Installer.Product' $pk.PSPath $pn $pk.PSChildName $null
Add-Item 'Installer.Feature' "HKLM:\SOFTWARE\Classes\Installer\Features\$($pk.PSChildName)" $pn $pk.PSChildName $null
}
}
}
# ---------- 3. Perforce 自己的注册表、服务、目录 ----------
foreach ($k in 'HKLM:\SOFTWARE\Perforce', 'HKLM:\SOFTWARE\WOW6432Node\Perforce') {
Add-Item 'Perforce.Reg' $k $k $null $null
}
$svcs = @(Get-CimInstance Win32_Service | Where-Object {
$_.Name -like 'Perforce*' -or $_.Name -like 'p4*' -or $_.Name -like '*helix*' -or
$_.PathName -match 'p4s\.exe|p4d\.exe|p4broker\.exe|p4p\.exe' })
$dirs = New-Object System.Collections.Generic.List[string]
$dirs.Add("$env:ProgramFiles\Perforce")
if (${env:ProgramFiles(x86)}) { $dirs.Add("${env:ProgramFiles(x86)}\Perforce") }
foreach ($i in $items | Where-Object { $_.Type -eq 'Uninstall' -and $_.Id -match '^\{' }) {
$dirs.Add("$env:ProgramData\Package Cache\$($i.Id)") # 安装包缓存, 残留会让安装器误判
}
if ($DeleteData) {
$dirs.Add("$env:ProgramData\Perforce"); $dirs.Add("$env:SystemDrive\Perforce")
$dirs.Add("$env:SystemDrive\p4root"); $dirs.Add("$env:SystemDrive\P4ROOT")
}
$dirs = @($dirs | Where-Object { $_ -and $_.Length -gt 3 } | Sort-Object -Unique | Where-Object { Test-Path -LiteralPath $_ })
# ---------- 汇总 ----------
Say "=== 检测结果 ===" Yellow
if ($items.Count -eq 0 -and $svcs.Count -eq 0 -and $dirs.Count -eq 0) {
Say "没有找到任何 Perforce/Helix 服务器安装登记。" Green
Say "说明 '已安装' 提示不是来自注册表, 请把提示原文和安装程序文件名发给我。" Yellow
exit 0
}
$items | ForEach-Object { Write-Host (" [{0}] {1} ({2})" -f $_.Type, $_.Name, $_.Key) }
foreach ($s in $svcs) { Write-Host " [Service] $($s.Name)" }
foreach ($d in $dirs) { Write-Host " [Dir] $d" }
if (-not $DeleteData) { Say "(默认不删除 P4ROOT/数据目录; 需要请加 -DeleteData)" DarkYellow }
if (-not $Yes) {
if ((Read-Host "确认清除以上内容? 输入 yes 继续") -ne 'yes') { Say "已取消"; exit 0 }
}
# ---------- 执行 ----------
Say ">>> [1/5] 尝试正常卸载 (MSI)..."
foreach ($i in $items | Where-Object { $_.Type -eq 'Uninstall' -and $_.Id -match '^\{[0-9A-Fa-f-]{36}\}$' -and $_.Uninstall -match 'msiexec' }) {
Write-Host " msiexec /x $($i.Id)"
$pr = Start-Process msiexec.exe -ArgumentList "/x $($i.Id) /qn /norestart" -PassThru
if (-not $pr.WaitForExit(180000)) { Write-Host " 超时, 跳过" -ForegroundColor Yellow; $pr | Stop-Process -Force -ErrorAction SilentlyContinue }
}
Say ">>> [2/5] 停止并删除服务..."
foreach ($s in $svcs) {
Write-Host " $($s.Name)"
if ($s.ProcessId -gt 0) { & taskkill.exe /F /PID $s.ProcessId 2>&1 | Out-Null }
& sc.exe stop $s.Name 2>&1 | Out-Null
& sc.exe delete $s.Name 2>&1 | Out-Null
Remove-Item "HKLM:\SYSTEM\CurrentControlSet\Services\$($s.Name)" -Recurse -Force -ErrorAction SilentlyContinue
}
foreach ($n in 'p4d', 'p4s', 'p4broker', 'p4p') { & taskkill.exe /F /IM "$n.exe" 2>&1 | Out-Null }
Say ">>> [3/5] 删除注册表登记..."
foreach ($i in $items) {
if (Test-Path -LiteralPath $i.Key) {
Write-Host " $($i.Key)"
Remove-Item -LiteralPath $i.Key -Recurse -Force -ErrorAction SilentlyContinue
}
}
Say ">>> [4/5] 删除目录..."
foreach ($d in $dirs) {
if (Test-Path -LiteralPath $d) {
Write-Host " $d"
Remove-Item -LiteralPath $d -Recurse -Force -ErrorAction SilentlyContinue
if (Test-Path -LiteralPath $d) { cmd /c "rd /s /q `"$d`"" 2>$null }
if (Test-Path -LiteralPath $d) { Write-Host " ! 被占用, 重启后再清" -ForegroundColor Yellow }
}
}
Say ">>> [5/5] 复查..."
$left = @($items | Where-Object { Test-Path -LiteralPath $_.Key })
if ($left.Count -eq 0) { Say "登记已全部清除" Green }
else { Say "仍有残留 (可能被占用/权限问题), 请重启后再运行一次本脚本:" Yellow; $left | ForEach-Object { Write-Host " $($_.Key)" } }
Say "=== 完成。请先重启电脑, 再运行安装程序 ===" Green