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

PowerShell中-eq是否支持匹配多个文件扩展名?

PowerShell匹配多种文件扩展名的正确方式

你写的代码里用-eq “.cer .key”是行不通的,因为-eq是严格相等比较,只会匹配扩展名完全等于这个字符串的文件,显然不是你要的效果。

要同时匹配.cer和.key两种扩展名,给你几个实用的方法:

  • 用-in运算符,将目标扩展名放到数组中做匹配:

    $certificates = Get-ChildItem -Path $using:certPath -Recurse | Where-Object { $_.Extension -in ".cer", ".key" }
    
  • 用-match结合正则表达式匹配:

    $certificates = Get-ChildItem -Path $using:certPath -Recurse | Where-Object { $_.Extension -match '\.(cer|key)$' }
    

    注:正则里的\.是转义点号(点在正则中是通配符),$表示匹配字符串结尾,确保只匹配完整的扩展名。

  • 更高效的方式:直接在Get-ChildItem中使用-Include参数,省去后续过滤步骤:

    $certificates = Get-ChildItem -Path $using:certPath -Recurse -Include "*.cer", "*.key"
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 18:04:52