PowerShell Script to Find and Replace Text in a File
Use
(Get-Content -Path 'file.txt') -replace 'oldText', 'newText' | Set-Content -Path 'file.txt' to find and replace text in a file with PowerShell.Examples
Inputfile.txt content: 'Hello world'
Outputfile.txt content after replace: 'Hello PowerShell'
Inputfile.txt content: 'apple, banana, apple'
Outputfile.txt content after replace: 'orange, banana, orange'
Inputfile.txt content: 'No match here'
Outputfile.txt content after replace: 'No match here'
How to Think About It
To replace text in a file, read the file content as text, use the
-replace operator to swap the old text with the new text, then write the updated content back to the file. This approach treats the file as plain text and updates all occurrences.Algorithm
1
Read the entire content of the file into memory.2
Replace all occurrences of the target text with the new text.3
Write the modified content back to the same file.Code
powershell
Get-Content -Path 'file.txt' | ForEach-Object { $_ -replace 'oldText', 'newText' } | Set-Content -Path 'file.txt' Write-Output "Replacement complete."
Output
Replacement complete.
Dry Run
Let's trace replacing 'apple' with 'orange' in a file containing 'apple, banana, apple'.
1
Read file content
'apple, banana, apple'
2
Replace 'apple' with 'orange'
'orange, banana, orange'
3
Write updated content back to file
File now contains 'orange, banana, orange'
| Step | Content |
|---|---|
| 1 | apple, banana, apple |
| 2 | orange, banana, orange |
| 3 | orange, banana, orange |
Why This Works
Step 1: Reading the file
The Get-Content command reads the file line by line as text.
Step 2: Replacing text
The -replace operator swaps all instances of the old text with the new text in each line.
Step 3: Saving changes
The updated lines are sent to Set-Content which overwrites the original file with the new content.
Alternative Approaches
Using -Raw parameter for whole file as one string
powershell
$content = Get-Content -Path 'file.txt' -Raw; $content = $content -replace 'oldText', 'newText'; Set-Content -Path 'file.txt' -Value $content Write-Output "Replacement complete."
This reads the entire file as one string, which is faster for large files but uses more memory.
Using StreamReader and StreamWriter for large files
powershell
$reader = [System.IO.StreamReader]::new('file.txt'); $writer = [System.IO.StreamWriter]::new('file.tmp'); while (($line = $reader.ReadLine()) -ne $null) { $writer.WriteLine($line -replace 'oldText', 'newText') } $reader.Close(); $writer.Close(); Remove-Item 'file.txt'; Rename-Item 'file.tmp' 'file.txt'; Write-Output "Replacement complete."
This method processes the file line by line without loading all content into memory, good for very large files.
Complexity: O(n) time, O(n) space
Time Complexity
The script reads and processes each line once, so time grows linearly with file size.
Space Complexity
The entire file content is stored in memory during processing, so space grows with file size.
Which Approach is Fastest?
Using -Raw is faster for medium files, but streaming line-by-line uses less memory for very large files.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Pipeline with Get-Content | O(n) | O(n) | Small to medium files |
| Get-Content -Raw | O(n) | O(n) | Medium files, faster processing |
| StreamReader/StreamWriter | O(n) | O(1) | Very large files with limited memory |
Always back up your file before running find and replace scripts to avoid accidental data loss.
Forgetting to write the modified content back to the file, so changes appear only in the console but not saved.