批量删除证书

在powershell 里面执行
<#
彻底删除 Fiddler 证书(删完立刻查不到)
#>
$IssuerFilter = "DO_NOT_TRUST_FiddlerRoot"
$logPath = ".\CertClean-Ultimate.log"
# 所有证书库(关键:加上 LocalMachine)
$stores = @(
"Cert:\CurrentUser\My",
"Cert:\CurrentUser\Root",
"Cert:\LocalMachine\My",
"Cert:\LocalMachine\Root"
)
$now = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
Add-Content $logPath "`n============================================="
Add-Content $logPath "执行时间: $now"
Add-Content $logPath "颁发者: $IssuerFilter"
Add-Content $logPath "============================================="
function Log($msg) {
Write-Host $msg
Add-Content $logPath $msg
}
# 1. 关 Fiddler、浏览器
Log "`n[1/6] 关闭 Fiddler/浏览器..."
Get-Process fiddler,chrome,msedge,firefox -EA SilentlyContinue | Stop-Process -Force
# 2. 停 CryptSvc 清系统证书缓存(非常关键)
Log "`n[2/6] 停止 Cryptographic Services 清缓存..."
net stop cryptsvc 2>&1 | Out-Null
# 3. 删证书(全库、不碰私钥)
foreach ($store in $stores) {
Log "`n扫描:$store"
$certs = Get-ChildItem $store -Recurse -EA SilentlyContinue |
Where-Object { $_.Issuer -match $IssuerFilter }
if (-not $certs) {
Log "→ 无匹配证书"
continue
}
Log "→ 找到 $($certs.Count) 张,删除..."
foreach ($cert in $certs) {
Log "指纹: $($cert.Thumbprint)"
try {
Remove-Item $cert.PSPath -Force -EA Stop
Log "✅ 已删除"
} catch {
Log "❌ 失败: $_"
}
}
}
# 4. 清 RSA 私钥残留
Log "`n[4/6] 清理 RSA 私钥目录..."
Remove-Item "$env:APPDATA\Microsoft\Crypto\RSA\*" -Force -Recurse -EA SilentlyContinue
Remove-Item "C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys\*" -Force -Recurse -EA SilentlyContinue
# 5. 重启 CryptSvc
Log "`n[5/6] 重启 Cryptographic Services..."
net start cryptsvc 2>&1 | Out-Null
# 6. 验证(全库查)
Log "`n[6/6] 验证残留..."
$allGood = $true
foreach ($store in $stores) {
$check = Get-ChildItem $store -Recurse -EA SilentlyContinue |
Where-Object { $_.Issuer -match $IssuerFilter }
if ($check) {
Log "❌ $store 残留 $($check.Count) 张"
$allGood = $false
}
}
if ($allGood) {
Log "`n✅ 全部删除干净,查询不到!"
}
Log "`n============================================="
Log "完成!建议重启电脑。"