用 Select-String 搜索文本

跨文件查找匹配行,相当于 PowerShell 的 grep。

Windows搜索文本PowerShell低风险
运行 Select-String -Path *.log -Pattern "error",列出匹配行。

01命令

命令
$ Select-String -Path *.log -Pattern "error"

02作用说明

Select-String 接受文件路径或管道中的文件以及匹配模式,返回包含文件、行号和文本的匹配对象。配合 Get-ChildItem -Recurse 可搜索整棵目录树。

03风险

风险低风险
使用条件Windows 10/11 with PowerShell 5.1 or PowerShell 7+
!注意: 模式默认按正则表达式处理,点号和星号等有特殊含义;纯文本查找请用 -SimpleMatch。

04撤销

✓无需撤销——该命令不会改变持久状态。

05来源

  • Select-String
    Microsoft Learn · vendor-doc · 2026-09-20
    依据: Finds text and regular-expression matches in strings and files, like grep.

最后验证: 2026-09-20 · 分类: 文件与文件夹

06相关方案