Skip to content

How to find and replace with regex across a whole folder on Windows

Change hundreds of files with one pattern, safely: see every match before anything is written, approve the changes, and keep a way back.

We make Regex Workbench, the app this guide uses. The free methods come first; every step shown in Regex Workbench was checked in the app itself, and the screenshots come from the real app with invented sample data.

Short answer

Work in two passes. First list every match in the folder without changing anything, and tighten the pattern until the list holds only what you mean. Then replace, with a copy of each file you change. VS Code and PowerShell can do both for free; Regex Workbench adds a tick box for every change, verified backups and an undo for the whole run.

What goes wrong in a folder-wide replace

  • The pattern matches more than you meant. In our test folder, http://brightfern\.example also matched a link to brightfern.example.net, a different site.
  • Letter case. PowerShell's -replace and Select-String ignore case unless you ask otherwise (about_Comparison_Operators): use -creplace and -CaseSensitive.
  • Encodings and line ends. A tool that rewrites a file can silently change its encoding, its byte order mark or its Windows line ends (CRLF). In Windows PowerShell 5.1, Set-Content and Out-File do not even agree on a default encoding (about_Character_Encoding).
  • No way back. Without a copy or version control, a bad pattern damages every file it touches.

Method 1 (free): VS Code's search across files

VS Code documents this in Basic editing: Search across files. Open the folder, press Ctrl+Shift+F, turn on regular expressions in the search box, and use the files to include and exclude to keep out folders such as node_modules. Type the replacement, and VS Code shows a diff of the pending changes before you replace. It keeps no backup of its own, so use Git or a copy of the folder. We cite these steps from VS Code's documentation; they were not part of our tests.

Method 2 (free, built in): PowerShell, with a dry run first

Windows PowerShell 5.1 comes with Windows 10 and 11. Everything below was tested on 30 November 2026 on a small demo folder: UTF-8 files with and without a byte order mark, Windows and Unix line ends, a UTF-16 file, an old ANSI file, and a node_modules folder. Put your own folder on the first line of each script. Step 1 lists every match and changes nothing (Select-String reports the file, the line number and the line):

$folder = "$env:USERPROFILE\Documents\Brightfern docs"
$pattern = 'http://brightfern\.example(?![\w.-])'
$types = '.md', '.html', '.txt', '.csv'
Get-ChildItem -LiteralPath $folder -Recurse -File |
  Where-Object { $types -contains $_.Extension -and $_.FullName -notmatch '\\(\.git|node_modules)\\' } |
  Select-String -Pattern $pattern -CaseSensitive -AllMatches |
  Format-Table Filename, LineNumber, @{n='Matches'; e={$_.Matches.Count}}, Line -AutoSize -Wrap

With the first pattern, http://brightfern\.example, the list showed 3 matches on the line of index.html, one of them the .net link. The end (?![\w.-]) means "not followed by a letter, a digit, an underscore, a dot or a hyphen", and the same line then showed 2. Filter the file types with Where-Object as above: in our test, -Include *.md combined with -LiteralPath matched every file in Windows PowerShell 5.1, backups included.

Step 2 replaces, writing a .bak copy of each file before changing it:

$folder = "$env:USERPROFILE\Documents\Brightfern docs"
$pattern = 'http://brightfern\.example(?![\w.-])'
$replacement = 'https://brightfern.example'
$types = '.md', '.html', '.txt', '.csv'
$strictUtf8 = New-Object System.Text.UTF8Encoding($false, $true)
Get-ChildItem -LiteralPath $folder -Recurse -File |
  Where-Object { $types -contains $_.Extension -and $_.FullName -notmatch '\\(\.git|node_modules)\\' } |
  ForEach-Object {
    $file = $_.FullName
    $bytes = [System.IO.File]::ReadAllBytes($file)
    $bom = $bytes.Length -ge 3 -and $bytes[0] -eq 0xEF -and $bytes[1] -eq 0xBB -and $bytes[2] -eq 0xBF
    $start = 0
    if ($bom) { $start = 3 }
    try { $text = $strictUtf8.GetString($bytes, $start, $bytes.Length - $start) }
    catch { Write-Warning "Skipped, not UTF-8: $file"; return }
    $new = $text -creplace $pattern, $replacement
    if ($new -cne $text) {
      Copy-Item -LiteralPath $file -Destination "$file.bak"
      [System.IO.File]::WriteAllText($file, $new, (New-Object System.Text.UTF8Encoding($bom)))
      "Changed: $file"
    }
  }

It reads each file as strict UTF-8 and writes it back with the same byte order mark, so only the matched text changes; line ends are part of the text and stay as they were. A file that is not valid UTF-8 (UTF-16, or an old ANSI file with accented letters) is skipped with a warning instead of being damaged. In our test it changed two files, skipped two, left node_modules alone, and a second run changed nothing. To reuse a captured group, write $1 in the replacement and keep it in single quotes, as Microsoft's page explains.

Method 3 (free): sed in Git Bash, with -b

If Git for Windows is installed, its Git Bash has grep and GNU sed. The GNU sed manual documents -i with a suffix (edit in place and keep a backup) and -E (extended regular expressions). Run from inside the folder:

grep -rlZE --include='*.md' --include='*.html' --include='*.txt' --include='*.csv' \
  --exclude-dir=node_modules --exclude-dir=.git 'http://brightfern\.example([^.[:alnum:]_-]|$)' . |
  xargs -0 sed -b -i.bak -E 's#http://brightfern\.example([^.[:alnum:]_-]|$)#https://brightfern.example\1#g'

Three things we saw with GNU sed 4.9 in Git Bash. Without -b, sed -i rewrote Windows line ends (CRLF) as Unix ones (LF) in every file it changed; with -b they were kept. Its patterns have no lookahead, so the pattern captures the next character and the replacement puts it back with \1. And grep did not find the match in the UTF-16 file, which was therefore left out without a word.

Method 4: Regex Workbench, to approve each change and undo a run

Regex Workbench is our Windows app for this job. It runs offline; the screenshots below come from version 1.0.0 with invented files.

  1. Write and test the pattern on sample text: matches are highlighted, and each part of the pattern is explained in plain words.
  2. Choose the folder on the Files tab, with the file types to search and the folders to leave out, such as .git and node_modules.
  3. Find (F5): every change is listed with its line before and after, and each file shows its encoding and line ends. Nothing is written yet.
  4. Untick the changes you do not want (Space), then replace the ticked ones (Ctrl+Enter). In the replacement, a captured group is written \1.
Regex Workbench Files tab: a pattern, a folder with subfolders, folders left out, and 8 changes in 4 files listed before and after, one of them unticked; nothing has been changed yet.
Find across a folder: 7 of 8 changes ticked in 4 files, each file with its encoding and line ends; nothing is written yet. Regex Workbench 1.0.0, captured on 27 November 2026, invented files.

Every Replace is a run. Before a file is written, its original is copied into the run's folder (under %LOCALAPPDATA%\AGI TOOLS\Regex Workbench\Runs), and each written file is read back after saving. The run keeps a report of every file and change, and it survives a restart.

Regex Workbench Runs tab: a run that wrote 4 files with 7 changes, each backed up first and read back, with buttons to undo the run, show the backups or open the report.
A run: 4 files written (7 changes), each backed up first and read back after saving. Regex Workbench 1.0.0, captured on 27 November 2026.

Undo run puts the original files back, unless a file was changed again after the run.

The same run marked as undone, with the list of the 4 files put back.
The run undone: 4 files put back. Regex Workbench 1.0.0, captured on 27 November 2026.
How to Test a Regex and Safely Find and Replace Across Files0:55Watch on YouTubeNothing loads from YouTube until you press play. The player then loads from youtube-nocookie.com, YouTube's privacy-enhanced mode, and connects to Google's servers. Privacy

Check the result

  • Run the dry run again. In our test, only the two skipped files still matched: those need an editor that keeps their encoding.
  • Compare a backup with the new file: fc.exe /n guide.md.bak guide.md prints the lines that differ.
  • Open one changed file of each type in the program that uses it (a browser for HTML, for example).
  • Delete the .bak files only when you are sure, and never let them sit where a web server publishes the folder.

Limitations

  • The PowerShell script handles UTF-8 files only and reads each file whole, so it suits text files of ordinary size. It was tested in Windows PowerShell 5.1, not in PowerShell 7.
  • sed works line by line, so a pattern cannot span two lines, and grep passes over files it takes for binary, such as UTF-16 ones.
  • Regex Workbench runs on Windows 10 (22H2) or 11, 64-bit, and Replace needs a licence. Undo run cannot restore a file that was changed again after the run.

References