PowerShell String Replace [With Examples]

Recently, I was doing some research on string replacements in PowerShell. In this tutorial, I have explained everything about the “PowerShell string replace” with examples.

To replace strings in PowerShell using the -replace operator, you can utilize its support for regular expressions to perform versatile text replacements. For example, to standardize phone numbers in various formats to (XXX) XXX-XXXX, you can use the following code: $_ -replace ‘(\d{3})[-. ]?(\d{3})[-. ]?(\d{4})’, ‘($1) $2-$3’.

Replace Strings in PowerShell

There are several ways to replace strings in PowerShell, each with its own use case. We’ll cover the following methods:

  1. The -replace operator
  2. The .Replace() method
  3. Using Regular Expressions with -replace

1. The -replace Operator

The -replace operator is the easiest way to replace text in strings. It supports regular expressions, making it suitable for complex pattern matching and replacements.

Let me show you an example.

Suppose you have a list of phone numbers in various formats, and you want to standardize them to the format (XXX) XXX-XXXX.

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

$standardizedNumbers = $phoneNumbers | ForEach-Object {
    $_ -replace '(\d{3})[-. ]?(\d{3})[-. ]?(\d{4})', '($1) $2-$3'
}

$standardizedNumbers

This script uses the -replace operator with a regular expression to match different phone number formats and replace them with the standardized format.

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

Replace Strings in PowerShell

Check out Replace Placeholders in Strings Using PowerShell

2. The .Replace() Method

The .Replace() method is another way to replace substrings within a string in PowerShell. It is case-sensitive and does not support regular expressions.

Here is an example to help you understand it better.

Suppose you have a list of city names where some of them are misspelled, and you need to correct them.

$cities = @(
    "New Yrok",
    "Los Angelez",
    "Chicgao",
    "Houston"
)

$correctedCities = $cities | ForEach-Object {
    $_.Replace("Yrok", "York").Replace("Angelez", "Angeles").Replace("Chicgao", "Chicago")
}

$correctedCities

This script corrects misspelled city names by chaining multiple .Replace() method calls.

Here is the output in the screenshot below:

PowerShell String Replace

Read Replace Multiple Characters in a String in PowerShell

3. Using Regular Expressions with -replace

Regular expressions provide a powerful way to perform complex replacements. The -replace operator supports regex, allowing for advanced pattern matching.

Here is an example.

Suppose you have a list of strings containing Social Security Numbers (SSNs), and you need to redact them for privacy.

$data = @(
    "John Doe: 123-45-6789",
    "Jane Smith: 987-65-4321",
    "Alice Johnson: 111-22-3333"
)

$redactedData = $data | ForEach-Object {
    $_ -replace '\d{3}-\d{2}-\d{4}', 'XXX-XX-XXXX'
}

$redactedData

This script uses a regular expression to match SSNs and replace them with a redacted format. You can see the output in the screenshot below:

How to Replace Strings in PowerShell

Conclusion

In this tutorial, I explained how to replace strings in PowerShell. We checked all the methods with examples.

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.