You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell如何删除PEM证书文件中以多个指定字符串开头的行

问题根因

你之前的写法不生效是因为 PowerShell 中的 -notmatch 为正则匹配运算符,仅支持传入单个匹配规则,无法直接接收多个字符串做批量匹配。你需要将所有需要排除的行规则合并为单个正则表达式,用 | 表示逻辑或即可。

正确实现代码

Get-Content "C:\ScriptRepository\Certs\CA-Chain.pem" | 
Where-Object { $_ -notmatch "^ |^subject|^issuer|^Bag Attributes" } | 
Set-Content "C:\ScriptRepository\Certs\CA-chain2.pem"

补充说明

上述正则的匹配规则覆盖了你所有需要删除的行:

  • 以空格开头的行 ^
  • 以subject开头的行 ^subject
  • 以issuer开头的行 ^issuer
  • 以Bag Attributes开头的行 ^Bag Attributes

如果需要指定输出文件的编码,可在 Set-Content 后添加 -Encoding UTF8 等参数指定编码格式,避免乱码。


内容的提问来源于stack exchange,提问作者Alexa Strange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 09:24:04