Deleting entire lines in a text file based on a partial string match with Windows PowerShell
- by Charles
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.