Welcome PowerShell User! This recipe is just one of the hundreds of useful resources contained in the Windows PowerShell Cookbook, 3rd edition.

If you own the book already, login here to get free, online, searchable access to the entire book's content.

If not, the Windows PowerShell Cookbook is available at Amazon, O'Reilly, or any of your other favourite book retailers. If you want to see what the PowerShell Cookbook has to offer, enjoy this free 90 page e-book sample: "The Windows PowerShell Interactive Shell".

Replace Text in a String

Problem

You want to replace a portion of a string with another string.

Solution

PowerShell provides several options to help you replace text in a string with other text.

Use the Replace() method on the string itself to perform simple replacements:

PS > "Hello World".Replace("World", "PowerShell")
Hello PowerShell

Use PowerShell’s regular expression -replace operator to perform more advanced regular expression replacements:

PS > "Hello World" -replace '(.*) (.*)','$2 $1'
World Hello

Discussion

The Replace() method and the -replace operator both provide useful ways to replace text in a string. The Replace() method is the quickest but also the most constrained. It replaces every occurrence of the exact string you specify with the exact replacement string that you provide. The -replace operator provides much more flexibility because its arguments are regular expressions that can match and replace complex patterns.

Given the power of the regular expressions it uses, the -replace operator carries with it some pitfalls of regular expressions as well.

First, the regular expressions that you use with the -replace operator often contain characters (such as the dollar sign, which represents a group number) that PowerShell normally interprets as variable names or escape characters. To prevent PowerShell from interpreting these characters, use a nonexpanding string (single quotes) as shown in the Solution.

Another, less common pitfall is wanting to use characters that have special meaning to regular expressions as part of your replacement text. For example:

PS > "Power[Shell]" -replace "[Shell]","ful"
Powfulr[fulfulfulfulful]

That’s clearly not what we intended. In regular expressions, square brackets around a set of characters means “match any of the characters inside of the square brackets.” In our example, this translates to “Replace the characters S, h, e, and l with ‘ful’.”

To avoid this, we can use the regular expression escape character to escape the square brackets:

PS > "Power[Shell]" -replace "\[Shell\]","ful"
Powerful

However, this means knowing all of the regular expression special characters and modifying the input string. Sometimes we don’t control that, so the [Regex]::Escape() method comes in handy:

PS > "Power[Shell]" -replace ([Regex]::Escape("[Shell]")),"ful"
Powerful

For extremely advanced regular expression replacement needs, you can use a script block to accomplish your replacement tasks, as described in Use a Script Block as a .NET Delegate or Event Handler. For example, to capitalize the first character (\w) after a word boundary (\b):

PS > [Regex]::Replace("hello world", '\b(\w)', { $args[0].Value.ToUpper() })
Hello World

For more information about the -replace operator, see Simple Operators and Appendix B.

See Also

Simple Operators

Appendix B

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.