How to replace multiple strings in text files in PowerShell
Ever had to do an unusual high number of find-replacements on an even higher numbered group of files? Remember despairing at the ticking clock while you ached through them one by one? We both know the pain then. Let’s avoid that in the future with some powershell sugar.
The little script below will run a mass find replace on all targeted files, using a lookup csv file to decide what strings to find and with what to replace them.
The Script
See the snippet below? Save it as Replace-InFilesUsingList.ps1
, or something else you prefer.
Param (
[String]$List = "ReplacementList.csv",
[String]$Files = ".Files*.*"
)
$ReplacementList = Import-Csv $List;
Get-ChildItem $Files |
ForEach-Object {
$Content = Get-Content -Path $_.FullName;
foreach ($ReplacementItem in $ReplacementList)
{
$Content = $Content.Replace($ReplacementItem.OldValue, $ReplacementItem.NewValue)
}
Set-Content -Path $_.FullName -Value $Content
}
The Lookup File
So how does the snippet above knows exactly what to find and replace? That’s where the lookup file comes in. As an example, save the text below as MyLookupFile.csv
in the same folder you saved the Replace-InFilesUsingList.ps1
script file.
OldValue,NewValue
MyFirstValueToFind,MyFirstValueToReplaceWith
MySecondValueToFind,MySecondValueToReplaceWith
MyThirdFileToFind,MyThirdValueToReplaceWith
You might have noticed there are no spaces or other odd characters in the file but that’s only to keep this example simple. It is a CSV file after all, so you can put whatever you wish, provided its values are *separated by commas&, as the name goes.
The Test Files
Make sure you know the files you want to target…
So you don’t end up mangling files you don’t really want to touch!
For this example, let’s create a collection of files to go along with the lookup file.
We’ll save the snippets below in their respective files under the Files
subfolder, just under where you saved the Replace-InFilesUsingList.ps1
script file.
FileToTarget1.txt
Somewhere herein lies MyFirstValueToFind just waiting to be found, not to mention MySecondValueToFind as well.
FileToTarget2.txt
Somewhere herein lies MySecondValueToFind just waiting to be found, not to mention MyThirdValueToFind as well.
FileToTarget3.txt
Somewhere herein lies MyFirstValueToFind just waiting to be found, not to mention MyThirdValueToFind as well.
FileNotToTarget.txt
Somewhere herein the lot of MyFirstValueToFind, MySecondValueToFind and MyThirdValueToFind are hiding in secret, hoping not to be found at all.
This covers some combinations of values in the files, just to be sure we can see different replacements being made. Note that the folder isn’t strictly required, it just helps you make sure you don’t touch anything else by accident.
The Test
Let’s give it a run then. Open up a PowerShell window, navigate to the folder where you put your script file and run the command below.
.Replace-InFilesUsingList.ps1 -List ".MyLookupFile.csv" -Files ".FilesFileToTarget*.txt"
Notice what’s happening here. We’re specifically telling the script to only lookup at the files we want it to look, using a file name mask.
Let’s check out what happened to the files then, one at a time.
FileToTarget1.txt
Somewhere herein lies MyFirstValueToReplaceWith just waiting to be found, not to mention MySecondValueToReplaceWith as well.
Success!
FileToTarget2.txt
Somewhere herein lies MySecondValueToReplaceWith just waiting to be found, not to mention MyThirdValueToReplaceWith as well.
Success as well!
FileToTarget3.txt
Somewhere herein lies MyFirstValueToReplaceWith just waiting to be found, not to mention MyThirdValueToReplaceWith as well.
We’re on a roll here!
FileNotToTarget.txt
Somewhere herein the lot of MyFirstValueToFind, MySecondValueToFind and MyThirdValueToFind are hiding in secret, hoping not to be found at all.
And it looks like our outlier file survived the find replace onslaught. Cool.