How to Replace Multiple Strings in a String Using PowerShell?

Recently, I was working on some strings in PowerShell to replace multiple strings in a string. I tried different ways to do it. In this tutorial, I will show you how to replace multiple strings in a string using PowerShell.

To replace multiple strings in a string using PowerShell, you can utilize the -replace operator within a loop. First, define a hashtable with the substrings to be replaced as keys and their replacements as values. Then, iterate over the hashtable keys and apply the -replace operator for each key-value pair. For example:

$string = "I have visited NY, CA, and TX."
$replacements = @{
    "NY" = "New York"
    "CA" = "California"
    "TX" = "Texas"
}

foreach ($key in $replacements.Keys) {
    $string = $string -replace $key, $replacements[$key]
}

Write-Output $string

This script will output: “I have visited New York, California, and Texas.”

I executed all the PowerShell scripts using VS code, but you can use any editor.

Method 1: Using -replace Operator

The first method to replace multiple strings in a string in PowerShell is by using the -replace operator.

The -replace operator uses regular expressions to replace substrings in PowerShell.

Here is a complete example.

Suppose you have a string containing state abbreviations and want to replace them with their full names. Below is the full script.

$string = "I have visited NY, CA, and TX."
$replacements = @{
    "NY" = "New York"
    "CA" = "California"
    "TX" = "Texas"
}

foreach ($key in $replacements.Keys) {
    $string = $string -replace $key, $replacements[$key]
}

Write-Output $string

Output:

I have visited New York, California, and Texas.

Even you can see the screenshot below for the output after I executed the above PowerShell script.

powershell replace multiple strings in string

Read Convert String to Boolean in PowerShell

Method 2: Using StringBuilder

You can also use the StringBuilder method when you are working with large strings in PowerShell. Let me show you how to use StringBuilder to replace multiple strings in a string using PowerShell.

Suppose you have a string with city names and you want to replace them with their respective states.

Add-Type -AssemblyName "System.Text"

$string = "I have been to New York City, Los Angeles, and Houston."
$replacements = @{
    "New York City" = "New York"
    "Los Angeles" = "California"
    "Houston" = "Texas"
}

$stringBuilder = New-Object System.Text.StringBuilder $string

foreach ($key in $replacements.Keys) {
    $stringBuilder.Replace($key, $replacements[$key])
}

Write-Output $stringBuilder.ToString()

Output:

I have been to New York, California, and Texas.

Method 3: Using Custom Function

You can also create a PowerShell custom function and reuse it to replace multiple strings.

Here is a complete example.

Let’s create a function to replace common abbreviations like “USA” with “United States of America” and “DC” with “District of Columbia”.

function Replace-MultipleStrings {
    param (
        [string]$inputString,
        [hashtable]$replacements
    )

    foreach ($key in $replacements.Keys) {
        $inputString = $inputString -replace $key, $replacements[$key]
    }

    return $inputString
}

$string = "The USA capital is DC."
$replacements = @{
    "USA" = "United States of America"
    "DC" = "District of Columbia"
}

$result = Replace-MultipleStrings -inputString $string -replacements $replacements
Write-Output $result

Output:

The United States of America capital is District of Columbia.

You can see the output in the screenshot below after I executed the above script using VS code.

Replace Multiple Strings in a String Using PowerShell

Read Find Lines Starting with a Specific String in PowerShell

Method 4: Using Regular Expressions for Complex Patterns

If you are working with complex patterns, then you can use regular expressions.

Here is an example to replace dates from MM/DD/YYYY to YYYY-MM-DD.

Suppose you have dates in the format MM/DD/YYYY and you want to convert them to YYYY-MM-DD.

$string = "The event is on 12/25/2023 and the deadline is 01/15/2024."
$pattern = "\b(\d{2})/(\d{2})/(\d{4})\b"
$replacement = '$3-$1-$2'

$result = $string -replace $pattern, $replacement
Write-Output $result

Output:

The event is on 2023-12-25 and the deadline is 2024-01-15.

Here is the output you can see in the screenshot below:

How to replace multiple strings in string in PowerShell

Conclusion

In PowerShell, you can replace strings in a string using various methods like -replace operator, StringBuilder, custom functions, etc. Leave me a comment below if it did not work as expected.

You may like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.