Piotr Stapp
Piotr Stapp

Reputation: 19830

Find files which does not contains selected string

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.

Upvotes: 23

Views: 25430

Answers (5)

Joachim Isaksson
Joachim Isaksson

Reputation: 180917

You can use Where-Object;

gci -filter "log.txt" | Where-Object { !( $_ | Select-String "something" -quiet) } | Select Fullname

This will find log.txt in all subdirectories, and output the full path of only those instances that do not contain "something" on any line.

Upvotes: 30

coder74309
coder74309

Reputation: 31

Try something like this:

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

In other words:

gci | ? {!($_.Name.Contains("Case-Sensitive-String"))}

Upvotes: 2

Sebastian Colon
Sebastian Colon

Reputation: 31

foreach($line in Get-Content .\file.txt) 
{
    if(findstr $line "dummyText.txt" ){
        # Work here or skip here
    }
    else {
     
      echo $line 
    
}

Upvotes: 0

Bohne
Bohne

Reputation: 4047

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"

Upvotes: 3

alroc
alroc

Reputation: 28174

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")){$_}}

Upvotes: 15

Related Questions