21

I am trying to find all files, which does not contains a selected string. Find files which contains is easy:

gci | select-string "something"

but I do not have an idea how to negate this statement.

Piotr Stapp
  • 19,392
  • 11
  • 68
  • 116

5 Answers5

27

You can use Where-Object;

gci | Where-Object { !( $_ | Select-String "something" -quiet) }
Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
15

I'm not sure if it can be done without the foreach-object but this works:

gci |foreach-object{if (-not (select-string -inputobject $_ -Pattern "something")){$_}}
alroc
  • 27,574
  • 6
  • 51
  • 97
  • I would also use the switch `-List`, i.e. `Select-String ... -List` in order to make this more effective, presumably, because we do not need all the found matches. – Roman Kuzmin Jul 30 '13 at 11:40
  • And one more thing. This code returns directories as well. In order to avoid this I would use `gci -File` (in PowerShell V3, at least). – Roman Kuzmin Jul 30 '13 at 11:42
  • 6
    `Where-Object` can make it even simpler. `gci -File | Where-Object {!(Select-String -InputObject $_ -Pattern "something" -List)}` – Roman Kuzmin Jul 30 '13 at 11:49
  • I found this "ls . -name yourfile*.txt | where {-not (select-string -path $_ -pattern "stringYouDontLike" -Quiet)}" on https://social.technet.microsoft.com/Forums/scriptcenter/en-US/b20748b9-57f2-474d-b90f-8d09fcf37fb7/get-filename-that-does-not-contain-the-specified-string?forum=winserverpowershell – Doug J. Huras Nov 08 '17 at 20:42
4

As mentionend in How do I output lines that do not match 'this_string' using Get-Content and Select-String in PowerShell?

Select-String has the NotMatch parameter.

So you could use it:

gci | Select-String -notmatch "something"
Community
  • 1
  • 1
Bohne
  • 4,029
  • 1
  • 16
  • 22
  • 2
    My guess is that the downvotes are due to this approach returning every single line in every single file that doesn't match the pattern specified. While I'm sure there's a use case for that, the question is asking for a way to just list the files that don't contain your search pattern, and this approach isn't very helpful for that. – Nick Ligerakis Apr 25 '18 at 13:40
  • This will always return `True`. Since there are blank lines in the output of `gci` (a.k.a. `Get-ChildItem`), there will always be lines that do not match the provided pattern. – Vince May 04 '21 at 08:16
1

Try something like this:

 Get-ChildItem | Where-Object {-Not($_.Name.Contains("Case-Sensitive-String"))}

In other words:

gci | ? {!($_.Name.Contains("Case-Sensitive-String"))}
coder74309
  • 11
  • 3
0
foreach($line in Get-Content .\file.txt) 
{
    if(findstr $line "dummyText.txt" ){
        # Work here or skip here
    }
    else {
     
      echo $line 
    
}
Shunya
  • 2,344
  • 4
  • 16
  • 28
  • 1
    Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Jun 15 '22 at 00:09