如何使用 PowerShell 脚本删除与 LocalMachine 存储中的证书关联的私钥?

How to delete a Private key associated with a certificate in LocalMachine store using PowerShell script?

提问人:Sarath G 提问时间:11/15/2023 最后编辑:litSarath G 更新时间:11/15/2023 访问量:25

问:

我想使用 Powershell 脚本删除与 LocalMachine\Root 中的证书关联的私钥。我需要首先使用其友好名称从商店中查找此证书,因为我们没有指纹。

我使用了 ,但它仅从 LocalMachine\Root 中删除证书,但私钥仍存在于 Personal 文件夹中。Remove-Item -DeleteKey

在搜索了许多论坛后,我找不到任何直接的答案。任何帮助都是值得赞赏的。

Powershell 版本 - 5.1,操作系统 - Windows 10

我尝试了以下代码,它只删除了根证书,而不是私钥。

$certFriendlyName = 'MyTestCertificate'
Set-Location Cert:\LocalMachine\Root
$cert = Get-ChildItem | Where-Object {$_.FriendlyName -match $certFriendlyName}
$cert
$cert | Remove-Item -DeleteKey

如果我不Set-Location,“-DeleteKey”开关不起作用并引发错误

A parameter cannot be found that matches parameter name 'DeleteKey'
PowerShell 脚本证书 私钥 证书存储

评论

0赞 bartonjs 11/16/2023
“但私钥仍存在于”个人“文件夹中。”如果您在 Root 中有一个副本,在 My/Personal 中有一个副本,则 Root 中的副本很可能不知道它有可用的私钥 ($cert。HasPrivateKey 可能为 false)。
0赞 bartonjs 11/16/2023
即使是错误的,“我有一个私钥”图标和“是否有元数据告诉我我的私钥在哪里”(CERT_PROP_KEY_PROV_ID),而不是“它是否存在”。因此,私钥可能会被删除,并且尝试使用它将从 My/Personal 中的副本中失败。$cert.HasPrivateKey$cert.HasPrivateKey

答: 暂无答案