在Powershell中如何精准查找扩展名为.asp的所有文件?
现象原因
PowerShell 中的 -Filter 参数使用的是 Windows 系统层面的通配符匹配逻辑,兼容旧版 DOS 8.3 文件名规则,*.asp 会匹配所有扩展名前3个字符为 asp 的文件,因此会额外命中 .aspx、.aspnet 这类后缀的文件。
兼容 PowerShell 2.1 的解决方法
方法1:Filter 结果二次过滤(推荐,兼顾性能)
先用 -Filter 缩小遍历范围,再通过文件扩展名属性精准匹配,比纯用 -Include 遍历效率更高:
Get-ChildItem C:\test -Filter *.asp -Recurse | Where-Object { $_.Extension -eq '.asp' } | % {$_.FullName}
方法2:直接使用 Include 参数
-Include 是 PowerShell 层面实现的过滤,匹配逻辑更严谨:
Get-ChildItem C:\test -Include *.asp -Recurse | % {$_.FullName}
注意:
-Include参数必须搭配-Recurse或者路径末尾加\*才会生效,适合目录下文件量不大的场景。
内容的提问来源于stack exchange,提问作者h_dml
相关产品推荐
相关产品推荐

