Recently, I was working with strings that contained special characters. We wanted to replace those special characters with one character. In this tutorial, I will show you how to replace special characters in a string in PowerShell using different methods.
To replace special characters in a string using PowerShell, you can utilize the -replace operator, which supports regular expressions. For example, to replace all non-alphanumeric characters with an underscore, you can use the following command: $newString = $originalString -replace '[^a-zA-Z0-9]', '_'. This method allows you to specify a pattern to identify the special characters and replace them efficiently.
Method 1: Using the -replace Operator
The PowerShell -replace operator is used to replace text in a string. Here, we can use regular expressions to find the special characters and replace them with one character.
Let me show you an example to understand it better.
Suppose you have an address string that includes special characters and want to replace them with spaces. Then, you can use the PowerShell script below.
$address = "1234 Main St., Apt #56, New York, NY 10001"
$cleanAddress = $address -replace '[#.,]', ' '
Write-Output $cleanAddressIn this example, the regular expression [#,\.] matches the characters #, . and ,, and replaces them with spaces. The output will be:
1234 Main St Apt 56 New York NY 10001You can even look at the screenshot below for the exact output after I executed the above PowerShell script.

Check out Convert String to JSON in PowerShell
Method 2: Using the String.Replace() Method
Now, let me show you another useful method. You can use the String.Replace() method to replace special characters in a string. It does not support regular expressions but is useful for replacing specific characters.
Here is an example.
Let’s say you have a Social Security Number (SSN) with hyphens, and you want to remove them. Here is the PowerShell script.
$ssn = "123-45-6789"
$cleanSSN = $ssn.Replace('-', '')
Write-Output $cleanSSNThis will output:
123456789Method 3: Using Regular Expressions with -replace
In PowerShell, you can use regular expressions with the -replace operator to match and replace patterns. Regular expressions are used in a little more complex scenarios.
Let me show you an example of replacing non-alphanumeric characters in a user name.
Suppose you have a username that may contain various special characters, and you want to keep only alphanumeric characters.
$username = "user@name#2024!"
$cleanUsername = $username -replace '[^a-zA-Z0-9]', ''
Write-Output $cleanUsernameThe regular expression [^a-zA-Z0-9] matches any character that is not alphanumeric and replaces it with an empty string. The result will be:
username2024I executed the above PowerShell script, and you can see the exact output in the screenshot below:

Check Convert String to Int in PowerShell
Replace Multiple Special Characters in a String using PowerShell
There might be scenarios where the user wants to replace multiple characters in a string using PowerShell.
Let me show you how to do this with an example.
Suppose you have a file path with various special characters and want to replace them with underscores. Then, you can use the PowerShell script below.
$filePath = "C:\\Users\\Bijay.Kumar\\Documents\\Report (Final) #2024.pdf"
$cleanFilePath = $filePath -replace '[\\\.\(\)#]', '_'
Write-Output $cleanFilePathThis regular expression [\\\.\(\)#] matches backslashes, dots, parentheses, and hash symbols, replacing them with underscores. The output will be:
C:_Users_Bijay_Kumar_Documents_Report _Final_ _2024.pdfYou can also see the exact output in the screenshot below:

Conclusion
I hope all these examples will help you to learn various methods to replace special characters in strings using PowerShell. Here, I have examples of how to use methods like using String.Replace, using the -replace operator and regular expressions to replace special characters in a string in PowerShell.
You may also like:
- PowerShell: IsNullOrEmpty vs IsNullOrWhiteSpace
- How to Substring in PowerShell?
- How to Replace Text in Strings Using PowerShell?
- Remove Characters from Strings in PowerShell
- Replace Multiple Characters in a String in 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.