It looks like you deal with literal strings. Do not use the -replace
operator
which deals with regular expressions. Use the Replace
method:
... | %{$_.Replace("string to replace", "replacement")} | ...
Alternatively, if you still want to use -replace
then also use [regex]::Escape(<string>)
. It will do the escaping for you.
Example: replacing text literally with "$_"
Compare the results of the following, showing what can happen if you use an automatic variable in a regex replacement:
[PS]> "Hello" -replace 'll','$_' # Doesn't work!
HeHelloo
[PS]> "Hello".Replace('ll','$_') # WORKS!
He$_o
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…