Home » PowerShell » Replace Pipe Character in String using PowerShell

Replace Pipe Character in String using PowerShell

Use the PowerShell String replace() method or replace operator to replace the pipe in a string with an empty space. e.g. $str.replace('|','') replace the pipe with a blank. Using replace operator, use the escape character followed by the pipe character to replace it.

The replace() method returns a string where the pipe character is replaced by an empty space.

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

Use replace() to Replace Pipe in String with 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 = "EmpId  | FirstName  | LastName | DOB"    

# Replace pipe character with an empty space
 $str.Replace('|','')                      

The above PowerShell script uses the replace() method to replace pipe character | 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 pipe character |
  2. the replacement string for the found text. In the above example, it is empty space

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

The output of the above script to replace pipe symbol with blank is:

PowerShell Replace Pipe Character in String
PowerShell Replace Pipe Character in String

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

Use replace Operator to Replace Pipe Character with Comma in PowerShell

Using the PowerShell replace operator, you can replace pipe characters in a string with a comma.

A Pipe character is a special character. replace operator uses the regex ( regular expression) for pattern matching, hence to find the pipe character, use escape character \

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

 $str = "EmpId  | FirstName  | LastName | DOB"   
      
# Replace pipe character with comma
$str -replace '\|',','       

The above PowerShell script uses the replace operator to replace pipe character | in a string with a comma.

replace operator takes 2 arguments:

  1. the substring to search for in a string. In the above example, it uses regex to find pipe symbol | using the escape character followed by a pipe character.
  2. the replacement string. In the above example, it uses comma , for replacement.

In the above example, replace operator replaces the pipe character in a string with a comma.

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

PS C:\> $str = "EmpId  | FirstName  | LastName | DOB"                                                               

PS C:\> $str -replace '\|',','
EmpId  , FirstName  , LastName , DOB

PS C:\>                                                                                

Cool Tip: How to replace parentheses in a string using PowerShell!

Conclusion

I hope the above article on how to replace pipe characters 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.