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.

WindowsFilespowershellsearch-replaceMedium risk
Pipe Get-ChildItem -Recurse results into ForEach-Object, then apply -replace to Get-Content -Raw output and write back with Set-Content.

01Command

Command
$ 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

RiskMedium risk
RequirementsPowerShell
!Warning: There is no undo — test the pattern on copies first, or add Copy-Item $_.FullName "$($_.FullName).bak" inside the loop before overwriting.

04Undo

✓No undo needed — this command does not change persistent state.

05Sources

  • about_Regular_Expressions | Microsoft Learn
    Microsoft · vendor-doc · 2026-09-17
    Evidence: Microsoft documents the -replace operator, which performs regex-based replacement and is case-insensitive in PowerShell by default.
  • The PowerShell Replace Operator
    SS64 · technical · 2026-09-17
    Evidence: 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