Home » PowerShell » PowerShell Replace Multiple Characters in String

PowerShell Replace Multiple Characters in String

Replace() function in PowerShell replace a character or string with another string and it returns a string. Since it returns the string, you can append replace() function at the end to replace multiple characters in a string.

You can replace multiple characters in a string using PowerShell replace() method or PowerShell replace operator.

If you are using the PowerShell replace() method, you can chain replace() method as many times to replace the multiple characters in the PowerShell string.

Like the replace() function, you can also chain PowerShell replace operator if you have multiple instances of characters to replace with.

In this article, we will discuss how to use PowerShell replace() method and PowerShell replace operator to replace multiple characters in a string.

Replace Multiple Characters using replace() method

PowerShell replace() method returns the string, hence you can append or chain as many replace() methods over the string to replace multiple characters in a string.

Let’s consider, that you have a string in PowerShell in variable $str as given below.

PS C:\> $str = "Hello. to Python Programming, Learn replace string in Python?" 

Hello. to Python Programming, Learn replace string in Python?

To replace multiple characters using the PowerShell replace() method like

replace . by empty space

replace , by !

replace ? by !

Refer to the following PowerShell script to replace characters.

$str.Replace('.','').Replace(',','!').Replace('?','!')  

In the above PowerShell script, we have used replace() method that takes two arguments; the character to find and the character to replace with the found character.

Since the replace() method returns the string, we have to chain together as many replace() methods to replace multiple characters in a string.

The output of the above PowerShell script after the replacement of multiple characters is:

PowerShell replace multiple characters
PowerShell replace multiple characters

Cool Tip: How to replace substring in a string using PowerShell!

Replace Multiple Characters in String using replace Operator

To replace multiple characters in a string using the PowerShell replace operator, you can chain together as many replace operators.

Refer to the above string example, where we want to replace multiple characters in a string using replace operator.

PS C:\> $str = "Hello. to Python Programming, Learn replace string in Python?" 

Hello. to Python Programming, Learn replace string in Python?

To replace multiple characters like below in PowerShell

replace . by empty space

replace, by !

replace ? by !

Refer to the following code, where it replaces the multiple instances of characters in a string using the PowerShell replace operator.

$str -replace '\.','' -replace ',','!' -replace '\?','!' 

In the above PowerShell script, we have to chain replace operator multiple times to replace multiple characters in a string.

PowerShell replace operator uses the regular expression (regex) for search, dot (.) and question mark (?) are the special characters. We need to use the escape character ‘\’

The output of the above PowerShell script is:

PS C:\> $str = "Hello. to Python Programming, Learn replace string in Python?"                                          
PS C:\>                                                                                                                 

PS C:\> $str -replace '\.','' -replace ',','!' -replace '\?','!'                                                        
Hello to Python Programming! Learn replace string in Python!

PS C:\>                                                                                                                           

Conclusion

I hope the above article on how to replace multiple characters in a string using the PowerShell replace() method and PowerShell replace operator is helpful to you.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.

Leave a Comment