Bulk Replace
How-to

How to Replace Text in Multiple Files With PowerShell

Copy-ready PowerShell commands to replace text in multiple files, with -replace vs .Replace(), encoding fixes, a safe dry run and backup steps.

Updated 5 minute read The Bulk Replace team

The quickest way to replace text in multiple files with PowerShell is to list the files with Get-ChildItem, read each one with Get-Content -Raw, change it with -replace or .Replace(), and write it back with Set-Content. The one-line version is below. The rest of this guide covers the details that decide whether it works cleanly: regex versus literal matching, case, encodings, and how to do a dry run first.

The basic command

Open PowerShell, move to your folder with cd, and run:

Get-ChildItem -Recurse -File -Filter *.txt | ForEach-Object { $t = Get-Content $_.FullName -Raw; $n = $t -replace 'old','new'; if ($n -ne $t) { Set-Content $_.FullName $n -NoNewline } }

  1. Get-ChildItem -Recurse -File -Filter *.txt finds every .txt file in the current folder and below. Drop -Recurse to stay in one folder.
  2. Get-Content -Raw returns the whole file as one string. Without -Raw you get an array of lines, which breaks multi-line patterns and changes the line endings when written back.
  3. $t -replace 'old','new' builds the new text.
  4. The if writes only files that changed, so untouched files keep their timestamps.
  5. -NoNewline stops Set-Content adding an extra line break at the end of the file.

You may see a shorter version online: (Get-Content file.txt) -replace 'old','new' | Set-Content file.txt. The brackets matter: they make PowerShell read the whole file before writing to it. Without them, it tries to write to a file that is still open for reading and fails.

-replace vs .Replace(): pick the right one

-replace-creplace.Replace()
Pattern typeRegular expressionRegular expressionLiteral text
CaseIgnores caseCase-sensitiveCase-sensitive
Capture groups$1, ${name}$1, ${name}No
Best forPatterns, case-insensitive changesPatterns where case mattersExact strings with dots, brackets or $

The classic mistake is using -replace with a literal string that contains regex characters. $t -replace 'example.com','example.org' also matches exampleXcom, because the dot means "any character". Either escape it ('example\.com'), wrap the search in [regex]::Escape('example.com'), or use $t.Replace('example.com','example.org').

Capture groups and the dollar sign

Always put the replacement in single quotes when it contains $1. In double quotes, PowerShell tries to expand $1 as a variable before the regex engine sees it, and you get an empty result. For example, to turn 31/12/2025 into 2025-12-31:

$n = $t -replace '(\d{2})/(\d{2})/(\d{4})','$3-$2-$1'

To insert a literal dollar sign in a -replace replacement, write $$. More group examples are in our guide to regex capture groups.

Encoding: the part that breaks files

Encoding is where most PowerShell replace jobs go wrong. The defaults differ by version:

  • Windows PowerShell 5.1 (the blue window built into Windows): Get-Content reads files without a byte order mark (BOM) as ANSI, and Set-Content writes ANSI by default. A UTF-8 file containing café can come out garbled.
  • PowerShell 7: reads and writes UTF-8 without a BOM by default, which suits most modern files.

The safe habit is to state the encoding on both read and write:

$t = Get-Content $f -Raw -Encoding UTF8; ...; Set-Content $f $n -NoNewline -Encoding UTF8

In 5.1, -Encoding UTF8 writes a BOM at the start of the file. Most tools ignore it, but some (older PHP setups, shell scripts, certain parsers) do not. If that matters, use PowerShell 7 with -Encoding utf8NoBOM, or write with [System.IO.File]::WriteAllText($f, $n), which writes UTF-8 without a BOM. If a file is UTF-16 or ANSI, read and write it with that encoding instead. If you have already hit this problem, see how to fix special characters broken after a find and replace.

Do a dry run first

PowerShell has no preview, so build one. This lists each file and how many matches it contains, without changing anything:

Get-ChildItem -Recurse -File -Filter *.html | ForEach-Object { $c = ([regex]::Matches((Get-Content $_.FullName -Raw), 'old')).Count; if ($c) { '{0} {1}' -f $c, $_.FullName } }

Note that [regex]::Matches is case-sensitive, unlike -replace. Add '(?i)old' to ignore case. For a line-by-line view of the matches, use:

Get-ChildItem -Recurse -File -Filter *.html | Select-String -Pattern 'old'

Many cmdlets also support -WhatIf. Adding -WhatIf to Set-Content prints what would be written to, without writing, which is a useful final check of the file list.

Back up before you write

Set-Content overwrites the file. There is no undo and nothing goes to the Recycle Bin. Before a real run:

  1. Commit the folder to Git, or
  2. Copy it: Copy-Item 'C:\site' 'C:\site-backup' -Recurse, or
  3. Save a copy of each file inside the loop before writing, for example Copy-Item $_.FullName ($_.FullName + '.bak'), and delete the .bak files once you are happy.

Several replacements in one pass

You can chain -replace operators, and they run left to right:

$n = $t -replace 'http://example\.com','https://example.com' -replace 'Acme Widgets','Acme Tools'

For a longer list, keep the pairs in a CSV with Find and Replace columns and loop over it:

$rules = Import-Csv rules.csv; ... foreach ($r in $rules) { $t = $t.Replace($r.Find, $r.Replace) } ...

Order matters: an earlier rule can create text that a later rule matches, so put longer, more specific terms first.

Save it as a reusable script

If you run the same kind of replace often, save the loop as a .ps1 file with parameters, for example param($Path, $Filter, $Find, $Replace), and call it as .\replace.ps1 -Path C:\site -Filter *.html -Find 'old' -Replace 'new'. If Windows refuses to run it, check the execution policy with Get-ExecutionPolicy; Set-ExecutionPolicy -Scope CurrentUser RemoteSigned allows local scripts for your account.

When a script is not the best tool

PowerShell is ideal for scheduled jobs and server automation. For one-off or repeated jobs at your desk, the missing pieces add up: no preview of the replaced result, no way to skip a single match, no undo, and encoding handling you have to get right yourself. Bulk Replace gives you those without a script: choose a folder and filters, add plain-text or regex rules (or import them from CSV), check a before/after diff, untick anything you want to keep, and apply. Bulk Replace backs up each file first, keeps its own encoding, and one click restores the whole run. For a fuller comparison, read find and replace tool vs PowerShell script.

Quick reference

  • Literal and case-sensitive: $t.Replace('a','b')
  • Regex, ignore case: $t -replace 'a','b'
  • Regex, match case: $t -creplace 'a','b'
  • Escape a literal for regex: [regex]::Escape('a.b')
  • Read whole file: Get-Content $f -Raw -Encoding UTF8
  • Write without extra newline: Set-Content $f $n -NoNewline -Encoding UTF8

Frequently asked questions

What is the difference between -replace and .Replace() in PowerShell?

-replace uses regular expressions and is case-insensitive by default. The .Replace() string method matches literal text and is case-sensitive.

Why did PowerShell change the encoding of my files?

Windows PowerShell 5.1 writes Set-Content output in the system ANSI code page by default, while PowerShell 7 writes UTF-8 without a BOM. Pass -Encoding explicitly to keep files consistent.

Can I undo a PowerShell replace?

No. Set-Content overwrites the file, so copy the folder or commit it to version control before you run the command.

Get three guides like this every month, plus software update notices.Subscribe →