FG
💻 Software

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

Fresh5 days ago
Mar 15, 2026141891 views
Confidence Score0%
0%

Problem

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 …

Error Output

PS C:\Users\Admin> (Get-Content "D:\Logs\co2.txt") | 
Foreach-Object {$_ -replace "3*Program*", ""} | 
Set-Content "D:\Logs\co2.txt"

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Fix for: Deleting entire lines in a text file based on a partial string match with Windows PowerShell

Low Risk

Here's what I would do: 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.

Awaiting Verification

Be the first to verify this fix

Sign in to verify this fix

Environment