When working with strings in PowerShell, I got a requirement to replace multiple characters or substrings to clean up or modify your data. In this tutorial, I will show you how to replace multiple characters in a string using PowerShell using various methods with examples.
To replace multiple characters in a string in PowerShell, you can use the -replace operator for pattern-based replacements, the String.Replace() method for straightforward substitutions, or a hashtable to manage multiple replacements efficiently. For example, you can replace state abbreviations with full names using the -replace operator: $string = $string -replace “NY”, “New York”.
1. Using the -replace Operator
The -replace operator is useful in PowerShell that allows you to replace text using regular expressions. This method is particularly useful when you need to replace patterns rather than fixed strings.
Let me show you an example.
Suppose you have a string containing state abbreviations and want to replace them with their full names. Here’s how you can do it:
# Original string with state abbreviations
$string = "NY, CA, TX, FL"
# Replace abbreviations with full names
$string = $string -replace "NY", "New York"
$string = $string -replace "CA", "California"
$string = $string -replace "TX", "Texas"
$string = $string -replace "FL", "Florida"
# Output the modified string
Write-Output $stringOutput:
New York, California, Texas, FloridaYou can also check out the screenshot below for the exact output after I executed the above script using VS code.

Check Concatenate Strings with New Line in PowerShell
2. Using the String.Replace() Method
The String.Replace() method is another way to replace characters or substrings in PowerShell. This method is case-sensitive and replaces all occurrences of a specified substring.
Let me show you an example of how to use the String.Replace() method.
Suppose you have a string with city names, and you want to replace them with different names:
# Original string with city names
$string = "Los Angeles, New York, Chicago, Houston"
# Replace city names
$string = $string.Replace("Los Angeles", "LA")
$string = $string.Replace("New York", "NYC")
$string = $string.Replace("Chicago", "Chi-Town")
$string = $string.Replace("Houston", "H-Town")
# Output the modified string
Write-Output $stringOutput:
LA, NYC, Chi-Town, H-TownYou can also check out the screenshot below for the output.

3. Using a Hashtable for Multiple Replacements
Using a hashtable can simplify the process when you need to replace multiple characters or substrings in a string in PowerShell. This method allows you to define all replacements in a single structure and apply them iteratively.
You can check the example below:
Consider a scenario where you need to replace various abbreviations with their full forms in a string:
# Original string with abbreviations
$string = "The GDP of USA is higher than that of UK and EU."
# Hashtable with replacements
$replacements = @{
"USA" = "United States of America"
"UK" = "United Kingdom"
"EU" = "European Union"
}
# Apply replacements
foreach ($key in $replacements.Keys) {
$string = $string.Replace($key, $replacements[$key])
}
# Output the modified string
Write-Output $stringOutput:
The GDP of United States of America is higher than that of United Kingdom and European Union.Read Concatenate String and Variable in PowerShell
4. Using Select-String with Regular Expressions
You can also use the Select-String cmdlet combined with regular expressions to replace multiple characters in a string in PowerShell.
Here is an example.
Suppose you have a string with dates in various formats, and you want to standardize them to MM/DD/YYYY:
# Original string with dates
$string = "Today is 2024-07-28, and the meeting is on 28/07/2024."
# Regular expression pattern for different date formats
$pattern = "\b(\d{4})-(\d{2})-(\d{2})\b|\b(\d{2})/(\d{2})/(\d{4})\b"
# Replace dates with standardized format
$string = [regex]::Replace($string, $pattern, {
if ($args[0] -match "(\d{4})-(\d{2})-(\d{2})") {
"{0}/{1}/{2}" -f $matches[2], $matches[3], $matches[1]
} elseif ($args[0] -match "(\d{2})/(\d{2})/(\d{4})") {
"{0}/{1}/{2}" -f $matches[1], $matches[2], $matches[3]
}
})
# Output the modified string
Write-Output $stringOutput:
Today is 07/28/2024, and the meeting is on 07/28/2024.You can see the output in the screenshot below after I executed the above script using VS code.

Conclusion
In this tutorial, I have explained how to replace multiple characters in a string in PowerShell using different methods. I recommend using the -replace Operator for this.
You may also like the following tutorials:
- Concatenate Strings Inside Loops in PowerShell
- Count Occurrences of a Substring in a String in PowerShell
- Replace Placeholders in Strings 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.