# Deleting entire lines in a text file based on a partial string match with Windows PowerShell

So I have several large text files I need to sort through, and remove all occurrences of lines which contain a given keyword. So basically, if I have these lines:

This is not a test
This is a test
Maybe a test
Definitely not a test

And I run the script with 'not', I need to entirely delete lines 1 and 4.

I've been trying with:

PS C:\Users\Admin> (Get-Content "D:\Logs\co2.txt") |
Foreach-Object {$_ -replace "3*Program*", ""} | Set-Content "D:\Logs\co2.txt"  but it only replaces the 'Program' and not the entire line. - ## 3 Answers Here's what I would do: Get-Content .\in.txt | Where-Object {$_ -notmatch 'not'} | Set-Content out.txt


Snark's line does the same, but it starts with loading all of the file into an array, which may be problematic with big files memory-wise.

-
I would suggest that you use Set-Content instead of Out-File, the former deals in raw data and won't cut off strings or format objects. – JasonMArcher Aug 17 '10 at 4:16
@JasonMArcher Can you post an example using Set-Content? I don't believe that Set-Content is a drop-in replacement for Out-File. – Iain Elder Nov 24 '12 at 22:05
Get-Content .\in.txt | Where-Object {$_ -notmatch 'not'} | Set-Content out.txt Out-File is basically Set-Content, but it runs the input through the default formatting instead of simple string conversion. – JasonMArcher Nov 24 '12 at 22:25 I had the exact same problem and you answer is 99% of what I need. The final issue is that I'm running the command in a loop with a$variable (replacing 'not' in your example). If I hard code a string it work perfectly; but if I use a variable it does nothing. Any idea why? I've tried both the bare variable and putting it in "quotes" – Stephen R May 20 at 16:47

This will work:

(Get-Content "D:\Logs\co2.txt") -notmatch "not" | Out-File "D:\Logs\co2.txt"

-
This just returns a single line in the second file named True. – Charles Mar 9 '10 at 4:33
Correction: This just returns a single line in the second file consisting of 'True'. Is it maybe missing the "Foreach-Object {$_ " so it runs once per line? Edit:Well actually it's irrelevant now, it erased the file I was hoping to clear up. Thank you for the help anyway, but I'm still curious what the correct command would be so I can do this in the future? – Charles Mar 9 '10 at 4:47 It works for me with your 4 test lines above. – Snark Mar 9 '10 at 6:24 would never have it overwrite the original file! – user33788 Jun 22 '10 at 16:06 agreed, but I used the same filenames as used by the original poster, to make the answer easier. He used co2.txt for both input and output. – Snark Jun 22 '10 at 16:54 You could also use 'Select-String' with the -notmatch option: Select-String 'not' .\input.txt -notmatch | % {$_.Line} | set-content output.txt

-