用 New-SelfSignedCertificate 创建自签名证书

一个 cmdlet 就是 Windows 内置的证书颁发机构——为本机 HTTPS 和 IIS 绑定签发测试 TLS 证书。

Windows证书安全PowerShell中风险
以管理员身份运行 `New-SelfSignedCertificate -DnsName dev.local -CertStoreLocation Cert:\LocalMachine\My` 即可为该域名签发证书。

01命令

命令
$ New-SelfSignedCertificate -DnsName dev.local -CertStoreLocation Cert:\LocalMachine\My

02作用说明

这个 cmdlet 会按你指定的 SAN 创建真正的 X.509 证书并直接放入所选存储——不需要 openssl 配置文件。默认生成的是现代密钥用法证书;用 `-NotAfter (Get-Date).AddYears(2)` 控制有效期,用 `-FriendlyName` 方便日后查找。浏览器仍会告警,除非把证书复制到受信任的根存储(Cert:\LocalMachine\Root),或在 IIS/Kestrel 里按指纹绑定。

03风险

风险中风险
使用条件Must run in an elevated PowerShell. Trust it by copying the cert to Cert:\LocalMachine\Root.
!注意: 自签名证书绝不能用于公网流量;把它加入根存储后,本机将信任该密钥签发的一切内容。

04撤销

撤销
$ Remove-Item Cert:\LocalMachine\My\<thumbprint>

05来源

  • New-SelfSignedCertificate | Microsoft Learn
    Microsoft · vendor-doc · 2026-09-17
    依据: Microsoft documents New-SelfSignedCertificate parameters -DnsName and -CertStoreLocation and notes it creates a self-signed test certificate.

最后验证: 2026-09-17 · 分类: 安全与隐私

06相关方案