Replace pipe character in string

The -replace operator uses regex matching. The | is a special character in regex. You need to escape it with a backslash \:

$line = $line -replace "\|", ";"

Although this should have worked too:

$line = $line.Replace("|", ";")

Note: Strings in .NET are immutable (why?). Operations like Replace don’t change the original string, so you have to assign the result back to $line.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top