How to Replace Special Characters in a String in PowerShell?

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 $cleanAddress

In 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 10001

You can even look at the screenshot below for the exact output after I executed the above PowerShell script.

Replace Special Characters in a String in PowerShell

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 $cleanSSN

This will output:

123456789

Method 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 $cleanUsername

The regular expression [^a-zA-Z0-9] matches any character that is not alphanumeric and replaces it with an empty string. The result will be:

username2024

I executed the above PowerShell script, and you can see the exact output in the screenshot below:

PowerShell Replace Special Characters in a String

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 $cleanFilePath

This 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.pdf

You can also see the exact output in the screenshot below:

Replace Multiple Special Characters in a String using PowerShell

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:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.