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:
- The
-replaceoperator - The
.Replace()method - 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'
}
$standardizedNumbersThis 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.

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")
}
$correctedCitiesThis script corrects misspelled city names by chaining multiple .Replace() method calls.
Here is the output in the screenshot below:

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'
}
$redactedDataThis script uses a regular expression to match SSNs and replace them with a redacted format. You can see the output in the screenshot below:

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:
- Replace Special Characters in a String in PowerShell
- Replace a Character in a String at a Specific Position in PowerShell
- Replace Multiple Strings in a String 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.