How to Replace Multiple Strings in an Array Using PowerShell?

Recently, I was doing some string manipulation and wanted to replace multiple strings in a PowerShell array. I tried four methods. In this tutorial, I will show you how to replace multiple strings in an array using PowerShell.

To replace multiple strings in an array using PowerShell, you can use the -replace operator, which supports regular expressions. For example, if you have an array of state abbreviations @(“CA”, “NY”, “TX”, “FL”), you can replace them with full state names using $states = $states -replace “CA”, “California”; $states = $states -replace “NY”, “New York”; $states = $states -replace “TX”, “Texas”; $states = $states -replace “FL”, “Florida”.

Method 1: Using the -replace Operator

The -replace operator in PowerShell is the easiest way to replace strings. It uses regular expressions. Here’s how you can replace multiple strings in an array using the -replace operator in PowerShell.

Let me show you an example.

Example

Suppose you have a string array of state names and abbreviations that you want to standardize by replacing abbreviations with full state names.

Below is the PowerShell script.

$states = @("CA", "NY", "TX", "FL", "CA", "NY")

# Replace state abbreviations with full names
$states = $states -replace "CA", "California"
$states = $states -replace "NY", "New York"
$states = $states -replace "TX", "Texas"
$states = $states -replace "FL", "Florida"

$states

In this example, we use the -replace operator to replace each state abbreviation with its full name.

Output:

California
New York
Texas
Florida
California
New York

I executed the above PowerShell script using VS code and it is showing me the exact output after replacing. Check out the screenshot below:

Replace Multiple Strings in an Array Using PowerShell

Check Array Contains in PowerShell

Method 2: Using the Replace() Method

The Replace() method is another way to replace strings in PowerShell. This method is case-sensitive and does not use regular expressions.

Let me show you an example of how to use the replace() method to replace multiple strings in an array in PowerShell.

Example

Suppose you have an array of city names with common typos that you want to correct.

$cities = @("Los Angles", "San Fransisco", "New Yrok", "Chcago")

# Correct common typos
$cities = $cities.ForEach{ $_.Replace("Los Angles", "Los Angeles") }
$cities = $cities.ForEach{ $_.Replace("San Fransisco", "San Francisco") }
$cities = $cities.ForEach{ $_.Replace("New Yrok", "New York") }
$cities = $cities.ForEach{ $_.Replace("Chcago", "Chicago") }

$cities

The ForEach method iterates over each element in the array, applying the Replace() method to correct the typos.

Output:

Los Angeles
San Francisco
New York
Chicago

Here you can see the output in the screenshot below after I executed the above PowerShell script.

PowerShell replace multiple strings in array

Check out How to Convert an Array to a String in PowerShell?

Method 3: Using a Hashtable for Bulk Replacements

For multiple replacements, especially when working with a large dataset, using a hashtable can make your script more efficient and maintainable. Let me show you an example.

Example

Let’s update a list of state abbreviations to their full names using a hashtable.

$states = @("CA", "NY", "TX", "FL", "CA", "NY")
$replacements = @{
    "CA" = "California"
    "NY" = "New York"
    "TX" = "Texas"
    "FL" = "Florida"
}

# Replace abbreviations using the hashtable
$states = $states.ForEach{ 
    $replacements.ContainsKey($_) ? $replacements[$_] : $_ 
}

$states

In this example, we use a hashtable to store the mappings of abbreviations to full names. The ForEach method checks if the current item exists in the hashtable and replaces it accordingly.

Output:

California
New York
Texas
Florida
California
New York

You can also check out the output in the screenshot below, you will get the exact output once you run the above PowerShell script.

replace multiple strings in array PowerShell

Method 4: Using Custom Functions

This is the final method. You can create a custom function that you can reuse to replace multiple strings in an array in PowerShell.

Here is an example.

Example

Consider an array of phone numbers in various formats that you want to standardize to a uniform format.

$phoneNumbers = @("(123) 456-7890", "123.456.7890", "123-456-7890", "1234567890")

function Normalize-PhoneNumber {
    param (
        [string]$number
    )
    # Remove non-numeric characters
    $number = $number -replace "\D", ""
    # Format as (XXX) XXX-XXXX
    return "({0}) {1}-{2}" -f $number.Substring(0,3), $number.Substring(3,3), $number.Substring(6)
}

# Normalize phone numbers
$phoneNumbers = $phoneNumbers.ForEach{ Normalize-PhoneNumber $_ }

$phoneNumbers

The Normalize-PhoneNumber function removes all non-numeric characters from the phone number and formats it in a standardized way. Using this function with the ForEach method ensures all phone numbers in the array are normalized consistently.

Output:

(123) 456-7890
(123) 456-7890
(123) 456-7890
(123) 456-7890

Conclusion

Replacing multiple strings in an array using PowerShell can be achieved through various methods, like by using the -replace operator, the Replace() method, a hashtable, or custom functions.

In this tutorial, I have explained each method with examples of how to replace multiple strings in an array in PowerShell.

You may also 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.