Replace / Remove

Replace Strings

Replaces text with new text and outputs to new file (From CMD terminal):

type C:\file.txt | powershell -Command "$input | % { $_ -replace 'oldtext', 'newtext' }" > C:\modifiedfile.txt

Replace a string in the file content

(Get-Content C:\path\to\file.txt) -replace 'oldString', 'newString' | Set-Content C:\path\to\modifiedfile.txt

Remove Strings

Remove Lines

type C:\file.txt | findstr /v "pattern" > C:\newfile.txt

Lines without "pattern"

type C:\file.txt | more +10 > C:\trimmedfile.txt

First 10 lines

type C:\file.txt | find /v "" > C:\nonemptyfile.txt

Non-empty lines

Remove Whitespace

Trim leading and trailing whitespaces from each line:

Get-Content C:\file.txt | ForEach-Object { $_.Trim() } | Set-Content C:\trimmedfile.txt

Last updated

Was this helpful?