Recently, someone asked me to write a complete PowerShell script to replace multiple strings in a file. In this tutorial, I will show you how to replace multiple strings in a file using PowerShell using different methods.
To replace multiple strings in a file using PowerShell, you can utilize the -replace operator in a chained manner. First, read the file content using Get-Content, then apply multiple -replace operations to update the desired strings, and finally, write the modified content back to the file using Set-Content. For example:
$filePath = "C:\MyFolder\file.txt"
$content = Get-Content $filePath
$content = $content -replace "OLD_STRING1", "NEW_STRING1" -replace "OLD_STRING2", "NEW_STRING2"
Set-Content -Path $filePath -Value $contentThis script efficiently replaces OLD_STRING1 and OLD_STRING2 with NEW_STRING1 and NEW_STRING2 respectively.
Replace Multiple Strings in a File Using PowerShell
This PowerShell script will be especially useful for system administrators and IT professionals managing Windows environments. It can be used to replace multiple strings in a file, such as configuration files, logs, or data files.
Method 1: Using the -replace Operator
The -replace operator is used to replace strings in PowerShell. You can chain multiple -replace operations together to replace multiple strings.
Let me show you an example.
Suppose you have a configuration file for a web application that needs to be updated. The file contains placeholders that need to be replaced with actual values:
ServerName: PLACEHOLDER_SERVER
DatabaseName: PLACEHOLDER_DBYou can use the following PowerShell script to replace these placeholders:
$filePath = "C:\Logs\config.txt"
$content = Get-Content $filePath
$content = $content -replace "PLACEHOLDER_SERVER", "us-west-server"
$content = $content -replace "PLACEHOLDER_DB", "us-west-db"
Set-Content -Path $filePath -Value $contentIn this example, PLACEHOLDER_SERVER is replaced with us-west-server and PLACEHOLDER_DB is replaced with us-west-db.
Check out Count Occurrences of a Substring in a String in PowerShell
Method 2: Using foreach Loop for Multiple Replacements
If you have a large number of replacements to make, using a foreach loop can make your script more manageable.
Here is an example of replacing multiple strings in a file using a PowerShell foreach loop.
Consider a log file that records events with placeholder values for sensitive information:
User: PLACEHOLDER_USER logged in from IP: PLACEHOLDER_IPYou can use a dictionary to store your replacements and a foreach loop to apply them:
$filePath = "C:\MyFolder\log.txt"
$content = Get-Content $filePath
$replacements = @{
"PLACEHOLDER_USER" = "JohnDoe"
"PLACEHOLDER_IP" = "192.168.1.1"
}
foreach ($key in $replacements.Keys) {
$content = $content -replace $key, $replacements[$key]
}
Set-Content -Path $filePath -Value $contentIn this script, PLACEHOLDER_USER is replaced with JohnDoe and PLACEHOLDER_IP is replaced with 192.168.1.1.
Method 3: Using -creplace for Case-Insensitive Replacements
The -creplace operator performs case-insensitive replacements, which can be useful if the text you are replacing may appear in different cases.
Here is an example.
Suppose you have a data file with inconsistent state abbreviations:
NY, new york
ca, California
TX, texasYou can use -creplace to standardize the state abbreviations:
$filePath = "C:\MyFolder\data.txt"
$content = Get-Content $filePath
$content = $content -creplace "ny", "NY"
$content = $content -creplace "ca", "CA"
$content = $content -creplace "tx", "TX"
Set-Content -Path $filePath -Value $contentOnce you execute the above PowerShell script, it will ensure all state abbreviations are capitalized uniformly. You can also see the output in the screenshot below:

Check out Replace Placeholders in Strings Using PowerShell
Method 4: Using StringBuilder for Large Files
For very large files, using StringBuilder can improve performance by avoiding the overhead of repeatedly modifying strings.
Example: Updating a Large CSV File
Consider a large CSV file with outdated city names:
City,State,Population
Los Angeles,CA,4000000
new york city,NY,8500000
chicago,IL,2700000You can use StringBuilder to efficiently replace the city names:
Add-Type -AssemblyName "System.Text"
$filePath = "C:\MyFolder\largefile.csv"
$content = [System.Text.StringBuilder]::new((Get-Content $filePath -Raw))
$content.Replace("new york city", "New York City")
$content.Replace("chicago", "Chicago")
[System.IO.File]::WriteAllText($filePath, $content.ToString())This script updates new york city to New York City and chicago to Chicago in a more performance-efficient manner.
Conclusion
PowerShell offers several methods for replacing multiple strings in a file. I have explained each method with examples here, and I hope it helps you.
You may also like:
- Replace Multiple Characters in a String in PowerShell
- Count the Number of Characters in a String in PowerShell
- Count Duplicate Lines in a Text File Using PowerShell
- PowerShell: Uppercase the First Letter of Each Word
- How to Find a String in a File and Return the Line Using PowerShell?
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.