title: How to escape and replace '\' in PowerShell
body: <p>We'd like to replace <code>\</code> to <code>\\</code> in our master files, which can prevent upload on Redshift (once replaced <code>\\</code> can be uploaded without issue and it would be uploaded single <code>\</code>, same as original customer data).</p>
  
  <p>I tried to replace <code>\</code> to <code>\\</code> as follows but received a regular expression error in PowerShell:</p>
  
  <pre><code>Param(
      [string]$TargetFileName
  )
  
  # replace words
  $old='`\'
  $new='`\`\'
  
  # replace \ to \\ for Redshift upload
  $file_contents=$(Get-Content "$TargetFileName") -replace $old,$new
  $file_contents &gt; $StrExpFile
  </code></pre>
  
  <p>Error Message:</p>
  
  <pre><code>+ $file_contents=$(Get-Content "$TargetFileName") -replace $old,$new
  +                  ~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : InvalidOperation: (`\:String) []、RuntimeException
      + FullyQualifiedErrorId : InvalidRegularExpression
  </code></pre>
  
  <p>Simply doing <code>-replace '\','\\'</code> didn't work either.</p>
  
  <p>We'd like to save it as the same file name, but the file size can be big, so if you have any better ideas, also would be so much appreciated.</p>