Home » PowerShell » Replace Parentheses in String using PowerShell

Replace Parentheses in String using PowerShell

Use the PowerShell String replace() method or replace operator to replace the parentheses in a string with an empty space. e.g. $str.replace('(','') replace the parentheses with a blank.

The replace() method returns a string where all parentheses are replaced by an empty space. To remove the parentheses pair, we will have to chain replace() method to replace open parentheses and close parentheses in a string.

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

Use replace() to Replace Parentheses with Empty Space in PowerShell

PowerShell String built-in replace() method takes two arguments; a string to search and a string to replace with the found text.

$str = 'PS-Para(1).png'    

# Replace all parentheses with an empty space
$str.Replace('(','').Replace(')','')                       

The above PowerShell script uses the replace() method to replace parentheses ( or ) in a string with an empty space.

PowerShell replace() takes 2 arguments:

  1. the substring to search for in the given string. In the above example, its parentheses ()
  2. the replacement string for the found text. In the above example, it is empty space

In the above example, we have parentheses pair, hence to replace open parentheses ( and close parentheses ), we will have to chain replace() to replace parentheses pair.

The PowerShell replace() method returns a new string where all the parentheses in a string are replaced by empty space.

The output of the above script to replace all parentheses with blank is:

PowerShell Replace Parentheses in String
PowerShell Replace Parentheses in String

Cool Tip: How to replace the first occurrence of a string in PowerShell!

Use replace Operator to Replace Parentheses with Space in PowerShell

Using the PowerShell replace operator, you can replace all parentheses in a string with an empty space

The replace operator in PowerShell takes two arguments; string to find for in a string and replacement string for the found text.

$str = 'PS-Para(1).png'     
      
# Replace all parentheses with an empty space
$str -replace '[()]',''        

The above PowerShell script uses the replace operator to replace parentheses in a string with an empty space.

replace operator takes 2 arguments:

  1. the substring to search for in a string. In the above example, it uses regex to find parentheses pair e.g. [()]
  2. the replacement string. In the above example, it uses ” ( empty space)

In the first example, replace operator replaces all parentheses with blank.

The output of the above Powershell script to replace parentheses in a string is:

PS C:\> $str = 'PS-Para(1).png'                                                              

PS C:\> $str -replace '[()]',''
PS-Para1.png                                                                                    

Cool Tip: How to replace all double quotes in a string using PowerShell!

Conclusion

I hope the above article on how to replace all parentheses in a string using PowerShell replace() method is helpful to you.

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