Batch find-and-replace text in files with PowerShell
A three-cmdlet pipeline that replaces a string across every file in a tree — the sed -i of Windows.
01Command
$ Get-ChildItem 'C:\Proj' -Filter *.txt -Recurse | ForEach-Object { (Get-Content $_.FullName -Raw) -replace 'old-domain.com','new-domain.com' | Set-Content $_.FullName }02What it does
-replace uses regular expressions, so characters like . and ( have to be escaped — use -creplace for case-sensitive matching since plain -replace ignores case. Reading with -Raw and writing back keeps line endings consistent. This pattern is standard for updating config files, connection strings, or copyright headers across a repo in one shot.
03Risk
04Undo
✓No undo needed — this command does not change persistent state.
05Sources
- about_Regular_Expressions | Microsoft LearnEvidence: Microsoft documents the -replace operator, which performs regex-based replacement and is case-insensitive in PowerShell by default.
- The PowerShell Replace OperatorEvidence: SS64 demonstrates find-and-replace across files using Get-ChildItem, Get-Content, -replace and Set-Content.
Last verified: 2026-09-17 · Category: Files & Folders
06Related solutions
Diagnose NTFS file system errors and bad sectors on any volume — including the system drive while Windows is running — before committing to a repair.
Robocopy is Windows' built-in robust file copier: use /MIR to keep a destination folder as an exact one-way mirror of a source, resumable and restartable.
Search inside text files and logs with regex, returning file, line number and match.