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
相关产品推荐
相关产品推荐

