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

如何生成仅文件名含指定短语的文件列表?PowerShell技术问询

仅筛选文件名包含特定短语的PowerShell方法

你之前用的Select-String是用来扫描文件内容匹配字符串的,这就是它会额外读取文件内容的原因——完全没必要用它来筛选文件名。下面是两种直接针对文件名筛选的高效方法:

方法一:用Get-ChildItem内置筛选(推荐,效率更高)

利用-Filter参数直接在文件系统层面完成筛选,避免后续管道处理,速度更快:

Get-ChildItem -Recurse -Filter "*Document1*" | Export-Csv "C:\temp\results.csv" -NoTypeInformation
  • *是通配符,匹配任意长度的任意字符,*Document1*表示文件名任意位置包含Document1
  • -NoTypeInformation用于移除CSV文件开头的类型注释,让输出更简洁

方法二:用Where-Object做灵活匹配

如果需要复杂匹配规则(比如正则表达式、区分大小写),可以用Where-Object过滤:

不区分大小写匹配(默认)

Get-ChildItem -Recurse | Where-Object { $_.Name -match "Document1" } | Export-Csv "C:\temp\results.csv" -NoTypeInformation

区分大小写匹配

Get-ChildItem -Recurse | Where-Object { $_.Name -cmatch "Document1" } | Export-Csv "C:\temp\results.csv" -NoTypeInformation

正则匹配示例(比如匹配以Document1结尾的文件名)

Get-ChildItem -Recurse | Where-Object { $_.Name -match "Document1$" } | Export-Csv "C:\temp\results.csv" -NoTypeInformation

可选:自定义CSV输出字段

如果只需要导出特定属性(比如完整路径、文件名),可以搭配Select-Object:

Get-ChildItem -Recurse -Filter "*Document1*" | Select-Object FullName, Name, Length | Export-Csv "C:\temp\results.csv" -NoTypeInformation

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 13:50:25