windows powershell 的 cert: 驱动器可直接管理本地证书,分 currentuser 和 localmachine 两级存储,支持 get-childitem 查看、import-pfxcertificate/export-certificate 导入导出、remove-item 安全删除。
windows 系统自带 powershell 的 cert: 驱动器,可以直接访问本地证书存储库,无需图形界面或第三方工具。核心是理解存储路径、权限层级和常用 cmdlet 的组合用法。
看清证书存在哪几个位置
Windows 证书按作用范围分为两类存储区:
-
Cert:\CurrentUser\*:当前用户级,比如
My(个人证书)、Root(受信任根)、TrustedPublisher(可信发布者),普通用户可读,部分操作需管理员权限才能写 -
Cert:\LocalMachine\*:本机级,如
LocalMachine\My、LocalMachine\Root,所有用户共享,修改必须以管理员身份运行 PowerShell
常见路径举例:Cert:\CurrentUser\My 存客户端证书;Cert:\LocalMachine\Root 存系统信任的根 CA(含 Charles、Fiddler、VMware 等工具生成的自签名根证书)。
用 Get-ChildItem 查看证书详情
这是最基础也最常用的命令,支持筛选、格式化和导出:
- 列出当前用户个人证书:
Get-ChildItem Cert:\CurrentUser\My - 只显示主题和指纹(便于识别):
Get-ChildItem Cert:\LocalMachine\Root | Select-Object Subject, Thumbprint, NotAfter - 查所有已过期的证书(NotAfter 早于今天):
Get-ChildItem Cert:\LocalMachine\Root | Where-Object { $_.NotAfter -lt (Get-Date) } - 导出为列表查看(含完整字段):
Get-ChildItem Cert:\CurrentUser\My | Format-List Thumbprint, Subject, Issuer, NotBefore, NotAfter, FriendlyName
导入和导出证书(PFX/CER)
适合批量部署或备份迁移:
- 从 PFX 文件导入(含私钥)到本机个人存储:
Import-PfxCertificate -FilePath "C:\cert.pfx" -CertStoreLocation Cert:\LocalMachine\My -Password (ConvertTo-SecureString "123456" -AsPlainText -Force) - 导出公钥证书(CER):
Get-ChildItem Cert:\LocalMachine\My -Thumbprint "A1B2..." | Export-Certificate -FilePath "C:\pub.cer" -Type CERT - 导出带私钥的 PFX:
Get-ChildItem Cert:\CurrentUser\My -Thumbprint "A1B2..." | Export-PfxCertificate -FilePath "C:\full.pfx" -Password (ConvertTo-SecureString "pass" -AsPlainText -Force)
安全删除证书(慎用)
删除前务必确认指纹,避免误删有效证书:
- 先查出目标证书(例如过期的根证书):
Get-ChildItem Cert:\LocalMachine\Root | Where-Object { $_.Subject -like "*Charles*" -and $_.NotAfter -lt (Get-Date) } - 复制其
Thumbprint,执行删除:Remove-Item Cert:\LocalMachine\Root\A1B2C3D4... - 删除后建议重启浏览器或服务(如 IIS、Edge/Chrome),否则旧证书可能仍被缓存使用
不复杂但容易忽略











