To remove the lines that contain the word pizza from the following text file:
The cat is my favorite animal. I prefer pizza to vegetables. My favorite color is blue. Tennis is the only sport I like. My favorite leisure time activity is reading books.
I ran the following code and it successfully removed the second line.
$inputFile = "C:\Temp\Text.txt"
Get-Content $inputFile | Where-Object {$_ -notmatch 'pizza'} | Set-Content "C:\Temp\Filtered.txt"
However, I haven't found a way to remove all occurrences of lines which contain either the word pizza or the word sport. I've tried to do this with this code:
$inputFile = "C:\Temp\Text.txt"
Get-Content $inputFile | Where-Object {$_ -notmatch 'pizza' -or $_ -notmatch 'sport'} | Set-Content "C:\Temp\Filtered.txt"
But it doesn't work as the output file is the same as the original one.
-andnot-or. Otherwise you are only filtering out lines with both pizza and sport.Where-Object {$_ -notmatch 'pizza|sport'}is a nicer way of matching multiple conditions