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 the below in PowerShell

replace . by empty space

replace, by !

replace ? by !

Refer to the following code, which 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:\>                                                                                                                           

PowerShell Replace All Characters in String

In PowerShell to replace all characters in a string with a single character, use the loop to iterate over each character in the string and replace it with a specified character.

$pwd = "ShellGeek@2023"                                                                                         $newString = ""                                                                                                 $replaceChar = "x"                                                                                              for($i=0;$i -lt $pwd.Length;$i++)
{
         $newString += $replaceChar
}                                                   

$newString   

The output of the above PowerShell script, replaces all characters in a string $pwd with x.

PS C:\> $newString                                                                                                      xxxxxxxxxxxxxx

PowerShell Get-Content to Read Content and Replace Multiple Strings in File

The Get-Content cmdlet in PowerShell is used to read the content of a file. To replace multiple strings in a file, use the replace operator.

In the following PowerShell script, the Get-Content cmdlet read the contents of the file and stores it in the $fileContent variable.

To replace multiple strings in a file, use the replace operator. The replace operator replaces the β€œEU” and β€œAP” with the β€œEU-01” and β€œAP-01” respectively in the file and stores the modified content in the $updatedContent variable.

The modified content passes through the pipeline operator to the Set-Content cmdlet to write to the file.

# Read the file using PowerShell Get-Content
$fileContent = Get-Content "D:\PS\comp.txt"

# use the replace operator to replace the multiple strings
$updatedContent = $fileContent -replace 'EU','EU-01' -replace 'AP','AP-01'

# use the Set-Content to save the updated content
$updatedContent | Set-Content "D:\PS\comp.txt"

In the above PowerShell script, multiple strings are replaced in a file and saved to the specified location.

The output of the above PowerShell script is:

PowerShell Get-Content - Replace Multiple Strings in File
PowerShell Get-Content – Replace Multiple Strings in File

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.