
如果您需要在Windows系统中通过PowerShell批量查看、导入或导出数字证书,而非依赖图形化证书管理器,则需调用Windows内置的Cert PowerShell模块及相关cmdlet。以下是完成这些操作的具体步骤:
一、查看本地计算机证书存储中的证书
PowerShell可通过Get-ChildItem访问证书存储路径,以枚举指定存储位置(如LocalMachine\My)下的所有证书对象,便于快速识别指纹、颁发者、有效期等关键属性。
1、以管理员身份运行PowerShell。
2、执行命令:Get-ChildItem -Path Cert:\LocalMachine\My 查看本机个人证书存储中的全部证书。
3、若需筛选过期证书,运行:Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object {$_.NotAfter -lt (Get-Date)}。
4、若仅显示证书主题与指纹,运行:Get-ChildItem -Path Cert:\LocalMachine\My | Select-Object Subject, Thumbprint。
二、从PFX文件导入证书到本地计算机存储
使用Import-PfxCertificate可将含私钥的PFX文件导入指定证书存储,支持自动选择存储位置并处理密码保护,适用于自动化部署场景。
1、确认PFX文件路径及访问权限,例如:C:\certs\server.pfx。
2、执行命令:Import-PfxCertificate -FilePath “C:\certs\server.pfx” -CertStoreLocation Cert:\LocalMachine\My -Password (ConvertTo-SecureString “YourPassword” -AsPlainText -Force)。
3、若需导入至受信任的根证书颁发机构存储,将-CertStoreLocation参数改为:Cert:\LocalMachine\Root。
4、验证是否成功导入:Get-ChildItem -Path Cert:\LocalMachine\My -Thumbprint “A1B2C3…”(替换为实际指纹)。
三、导出证书(不含私钥)为CER格式
导出公钥证书为CER文件适用于分发给客户端或添加至其他设备的信任列表,该操作不包含私钥,安全性高且无需密码。
1、获取目标证书对象,例如按指纹查询:$cert = Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object {$_.Thumbprint -eq “A1B2C3…”}。
2、执行导出命令:$cert | Export-Certificate -FilePath “C:\certs\exported.cer” -Type CERT。
3、检查导出文件是否存在且大小非零:Get-Item “C:\certs\exported.cer”。
四、导出证书(含私钥)为PFX格式
导出带私钥的PFX文件需提供强密码保护,仅限具备私钥读取权限的用户执行,常用于迁移或备份关键服务证书。
1、确保当前用户对证书私钥具有“读取”权限,否则导出将失败。
2、获取证书对象:$cert = Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object {$_.Thumbprint -eq “A1B2C3…”}。
3、执行导出命令:$cert | Export-PfxCertificate -FilePath “C:\certs\backup.pfx” -Password (ConvertTo-SecureString “StrongPass123!” -AsPlainText -Force)。
4、验证导出结果:Test-PfxCertificate -FilePath “C:\certs\backup.pfx” -Password (ConvertTo-SecureString “StrongPass123!” -AsPlainText -Force)。
五、删除指定证书
Remove-Item支持直接从证书存储中移除证书对象,操作不可逆,需谨慎确认指纹与存储路径,避免误删系统关键证书。
1、列出待删证书详细信息:Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object {$_.Subject -like “*example.com*”}。
2、复制目标证书的完整Thumbprint值。
3、执行删除命令:Remove-Item -Path “Cert:\LocalMachine\My\A1B2C3…” -DeleteKey(添加-DeleteKey可同时清除关联私钥)。
4、再次查询确认已不存在:Get-ChildItem -Path Cert:\LocalMachine\My -Thumbprint “A1B2C3…” 应无输出。

评论(0)