PowerShell: Escape characters in Replace and Contains operations
<p>I'm trying to replace all occurrences of <code>$(foo)\bar</code> with <code>$(foo)\baz\bar</code> literal string, I don't need to expand anything and I don't need to use regex. </p>

<p>This is my code:</p>

<pre><code>if($myString.contains("$(foo)\bar\"))
{
    $myString = $myString -replace "$(foo)\bar\","$(foo)\baz\bar\"
    ...
}
</code></pre>

<p>It doesn't work, I'm totally lost how, when and where to escape characters like <code>$</code>, <code>(</code>, <code>\</code> . Should I use single or double quotes in strings?</p>

<p>This is my attempt:</p>

<pre><code>if($myString.contains("`$(foo)\\bar\\"))
{
    $myString = $myString -replace '\$(foo)\\bar\\','\$(foo)\\baz\\bar\\'
    ...
}
</code></pre>