title: Why are all newlines gone after PowerShell's Get-Content, Regex, and Set-Content?
body: <p>I want to load a file template into a variable, modify data within the variable and output the modified template to a new location from the variable.</p>
  
  <p>The issue is that PowerShell is removing newlines from my template.</p>
  
  <p>The input file (template file) has Unix line endings which are also required for output since the recipient of the modified version is a Unix-based system.</p>
  
  <p>I have the following code which results into a concatted one-liner:</p>
  
  <pre><code>[String] $replacement = "Foo Bar"
  [String] $template = Get-Content -Path "$pwd\template.sh" -Encoding UTF8
  $template = $template -replace '&lt;REPLACE_ME&gt;', $replacement
  $template | Set-Content -Path "$pwd\script.sh" -Encoding UTF8
  </code></pre>
  
  <p>Having the template input:</p>
  
  <pre><code>#!/bin/sh
  myvar="&lt;REPLACE_ME&gt;"
  echo "my variable: $myvar"
  exit 0
  </code></pre>
  
  <p>Resulted into:</p>
  
  <pre><code>#!/bin/sh myvar="Foo Bar" echo "my variable: $myvar" exit 0
  </code></pre>
  
  <p>It appears to me that somewhere <code>LF</code> where replaced by one simple whitespace. Finally at the end of the script there is an added <code>CR LF</code> which was not present in the template file.</p>
  
  <p>How do I preserve the line endings and prevent adding further (<code>CR LF</code>) <em>wrong</em> line endings to the final script?</p>