Powershell select-string但有一个排除

时间:2015-02-17 17:32:23

标签: powershell powershell-v2.0

Get-ChildItem "C:\users\admin\Desktop\docs\" -recurse | 
    Select-String "smtp" | 
    group path | 
    select name

在该文件夹中的所有文件中查找单词SMTP,这很棒,但我想在该文件夹中查找具有SMTP但没有单词PDF的文件。

无法弄清楚如何做到这一点。

有什么想法吗?提前谢谢,

2 个答案:

答案 0 :(得分:0)

尝试使用-like-notlike

Get-ChildItem "C:\users\admin\Desktop\docs" -recurse | where $_.FullName -like "*smtp*" -and $_.FullName -notlike "*pdf*"

答案 1 :(得分:0)

您可以通过将过滤器应用于返回的Select-String对象的.line属性来“延迟过滤”matchinfo的结果:

Get-ChildItem "C:\users\admin\Desktop\docs\" -recurse | 
    Select-String "smtp" | 
    Where { $_.line -notlike '*PDF*' } |
    group path | 
    select name