1. Home
  2. Windows Tips
  3. Replace a word in multiple text files windows 10

How To Replace A Word In Multiple Text Files On Windows 10

Text file editors like Notepad and Notepad++ are used to create lots of different types of files like subtitles, log files, batch files, PowerShell scripts, and more. Where a text file editor can create these files, it can also edit them. If you have a lot of text files, ones that have the TXT file extension and you  need to replace a word, or several words in them you can do so with a PowerShell script. The script makes it so you don’t have to individually open each file and then replace the word. You can use this same script for  other file types that can be created with a text file editor. Here’s how you can replace a word in multiple text files.

Replace Word In Text Files

First, you need to put all your text files in the same folder. The script will examine only one directory when it runs and not your entire system which is why you need all the files in one place.

Open a new Notepad file and paste the following in it.

Get-ChildItem 'Path-to-files\*.txt' -Recurse | ForEach {
(Get-Content $_ | ForEach { $_ -replace 'Original-Word', 'New-Word' }) |
Set-Content $_

You need to edit the above script. First, edit the ‘Path-to-files’ part with the actual path to the folder with all the text files in it. Second, replace the ‘Original-Word’ with the word you want to replace. Finally, replace the ‘New-Word’ with the word you want to replace the old one with. For example, I have a few text files that all have the word ‘Post’ in them. I want to replace the word Post with Article. This is what the script will look like once I’ve edited it to suit my scenario.

Get-ChildItem 'C:\Users\fatiw\Desktop\notepad-files\*.txt' -Recurse | ForEach {
(Get-Content $_ | ForEach { $_ -replace 'Post', 'Article' }) |
Set-Content $_

Once you’ve edited the script, save it with the ps1 file extension. Make sure you change the file type from text files to all files in Notepad’s save as dialog. Run the script and it will perform the replace function.

If you want to use this same script for XML or LOG files, edit the file extension in the first line. For example,

This will become

Get-ChildItem 'C:\Users\fatiw\Desktop\notepad-files\*.txt'


Get-ChildItem 'C:\Users\fatiw\Desktop\notepad-files\*.xml'

There is one thing you ought to know about this script; it doesn’t match words to words. If you’re looking to replace every occurrence of ‘the’ with ‘a’, it will also replace the ‘the’ at the start of ‘these’ and ‘there’. That is a shortcoming of this script. To work around it, you can use Notepad++ which has a match word option.

1 Comment

  1. Wow! This was amazing!
    The .WS config files for my AS400 system macros. When sharing them with other users, the edit to tailor each file for each user is now easy. Thanks so much!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.