Home Β» PowerShell Β» PowerShell Replace Special Characters

PowerShell Replace Special Characters

Use the PowerShell replace() method and PowerShell replace operator to replace special characters in a given string.

PowerShell replace operator uses regular expression (regex) to search characters or strings, hence to replace a special character, use the escape character β€˜\β€˜ before the special character.

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

Replace Special Characters using replace() method

PowerShell replace() method easily replace the special characters in a given string. It takes two arguments; string to find for and string to replace with found text in a string.

Let’s say we have a string $string that contains the special character β€œHello? System Administrator.β€œ

$string = "Hello? System Administrators." 

# Chain replace() method to replace multiple special character
$string.Replace('?','!').Replace('.','!')    

In the above PowerShell script, the $string variable contains the string with special characters like ? and dot (.)

Using the PowerShell replace() method chaining together over the string, we can replace multiple characters in a string.

The output of the script after replacing the special characters in a PowerShell string is:

PowerShell replace special characters
PowerShell replace special characters

Replace Special Characters in a String using replace operator

PowerShell replace operator uses the Regular expression pattern matching. A dot (.) and question mark (?) are the special characters, hence to replace special characters in a string, use the escape character β€˜\’

Let’s consider the above string $string that contains the special characters in it. To replace a special character using the replace operator, refer to the following code.

$string = "Hello? System Administrators."     

# Use the escape character \ to replace special characters
$string -replace '\?','!' -replace '\.','!' 

The output of the above PowerShell script after using the replace operator with an escape character to replace special characters is:

PS C:\> $string = "Hello? System Administrators."                                                                       

PS C:\>                                                                                                                 

PS C:\> $string -replace '\?','!' -replace '\.','!'                                                                     
Hello! System Administrators!

PS C:\>                                                                                                                                                                                                                                                                                                                                                                                   

Conclusion

I hope the above article on how to replace special characters in PowerShell using the 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.