To find and replace in all files in a folder and its subfolders, you need a method that searches recursively: PowerShell with Get-ChildItem -Recurse, an editor's find in files with subfolders switched on, or a batch tool with an "include sub-folders" option. The recursion itself is easy. The hard part is controlling the scope, so the change reaches every file you want and none of the files you do not.
Below are the exact steps for each method, followed by the filters that keep a recursive replace under control.
Why recursive replaces need more care
A single folder holds a handful of files you can picture. A folder tree can hold thousands, including things you forgot were there: a node_modules or vendor folder full of third-party code, a hidden .git folder, old backups, minified copies, logs and images. A replace that sweeps through all of them can:
- Change library code you never meant to edit.
- Corrupt version control data inside
.git. - Damage binary files such as images, fonts or archives if the tool treats them as text.
- Take far longer than needed because it reads huge folders you do not care about.
So before choosing a method, write down three things: the top folder, the file extensions to include, and anything to leave out.
Method 1: PowerShell with -Recurse
Open PowerShell, then run this, changing the path, extensions and text to suit:
Get-ChildItem 'C:\site' -Recurse -File -Include *.html,*.css,*.php | ForEach-Object { $t = Get-Content $_.FullName -Raw -Encoding UTF8; $n = $t.Replace('old text','new text'); if ($n -ne $t) { Set-Content $_.FullName $n -NoNewline -Encoding UTF8; $_.FullName } }
How it works:
-Recursewalks every subfolder underC:\site.-Filereturns files only, not folder objects.-Includeaccepts several patterns separated by commas. (-Filteris faster but takes only one pattern.).Replace()does a literal, case-sensitive replacement. Use-replaceinstead if you need a regular expression.- The path of each changed file is printed, which gives you a simple log.
Hidden files and folders are skipped unless you add -Force, which is usually what you want. To leave out a folder such as node_modules, add a filter before the loop:
... -Include *.js | Where-Object { $_.FullName -notmatch '\\node_modules\\' } | ForEach-Object { ... }
The -Encoding UTF8 is there because Windows PowerShell 5.1 otherwise reads files without a byte order mark as ANSI and writes ANSI, which breaks accented characters. In 5.1, -Encoding UTF8 writes a BOM; in PowerShell 7 use utf8NoBOM if you need files without one. Our PowerShell replace guide explains the encoding options in full.
Method 2: an editor's find in files
VS Code
VS Code searches the open folder and all its subfolders by default. Open the top folder, press Ctrl+Shift+H, and use the two boxes under the search field:
- files to include: for example
*.html, *.css, or a subfolder such as./docs/**. - files to exclude: for example
**/vendor/**, *.min.js.
By default it also respects .gitignore and your search.exclude settings, so folders such as node_modules are skipped. Review the results, then click Replace All.
Notepad++
Press Ctrl+Shift+F, set the Directory to your top folder, and tick In all sub-folders. Leave In hidden folders unticked unless you really need it. In Filters, list the extensions separated by spaces, such as *.html *.css. Recent versions also accept exclusions, for example !*.min.js. Click Find All to check the matches before clicking Replace in Files.
Method 3: a batch tool with include sub-folders
A dedicated tool combines the recursion, the filters and a safety net in one screen. In Bulk Replace:
- Choose the top folder and tick include sub-folders. The whole tree is processed, ten files or ten thousand.
- Filter by extension (for example
.html .css .php), file size, modified date, and whether to include hidden files. - Add your find and replace rule, or several stacked rules.
- Check the preview. It lists every matching file in the tree with a total match count. Untick files that sit in folders you did not mean to touch.
- Apply. Files are backed up first, and one click undoes the whole job.
Bulk Replace detects binary files such as images and archives and skips them automatically, even if no extension filter is set.
Scope checklist
| Usually include | Usually exclude | Why |
|---|---|---|
| Source files: .html .css .js .php .py .cs | node_modules, vendor, packages | Third-party code is replaced on the next install anyway |
| Config: .ini .conf .xml .json | .git, .svn and other hidden folders | Version control data must not be edited by hand |
| Docs: .md .txt | *.min.js, *.min.css, build output | Generated files are rebuilt from the sources |
| Your own custom text formats | Images, fonts, archives, .docx, .pdf | Not plain text, so a text replace can damage them |
Test on a small subfolder first
For any replace that will touch hundreds of files, run it once on a single subfolder:
- Point the method at one small subfolder instead of the top folder.
- Run the replace and open the changed files.
- Check the output works: load the page, run the script or open the document.
- When you are happy, point it at the top folder and run it again.
This catches pattern mistakes when they affect five files, not five thousand.
Counting matches across the tree
Before and after a recursive replace, it helps to know how many files contain the text. In PowerShell:
Get-ChildItem 'C:\site' -Recurse -File -Include *.html | Select-String -SimpleMatch 'old text' -List | Measure-Object
-List stops at the first match in each file, so the count is the number of files, not the number of matches. Remove -List to count matching lines instead. For more ways to search, see how to find all files containing text on Windows.
Changing file and folder names too
Sometimes the text you are replacing also appears in file or folder names, for example an old product name in oldname-logo.png or a folder called oldname. Content replaces do not rename anything, so links can break if you only change one side. You can rename separately with PowerShell's Rename-Item, or use a tool that changes names and contents in the same run. Our guide to replacing text in file names covers both.
Quick recap
- Recursion is one switch:
-Recurse, In all sub-folders, or include sub-folders. - Control matters more: filter by extension, leave out vendor and hidden folders, and skip binaries.
- Count matches before and after, and test on a small subfolder first.
- Keep a backup, or use a tool that makes one for you, so a mistake in a large tree can be reversed.
Frequently asked questions
How do I include subfolders in a PowerShell replace?
Add -Recurse to Get-ChildItem, for example Get-ChildItem C:\site -Recurse -File -Filter *.html, and pipe the results into your replace loop.
How do I stop the replace touching images and other binary files?
Limit the run to the text extensions you need, such as *.html or *.css. Dedicated batch tools also detect binary files and skip them automatically.
Will a recursive replace change hidden folders like .git?
It can. Get-ChildItem only includes hidden items if you add -Force, but editors and other tools vary, so check your filters and exclude hidden files when you do not want them changed.
