title: Passing newline character to PowerShell via Cmd
body: <p>I'm trying to run a PowerShell script from Windows cmd.exe.  The input to the PowerShell script is a string, which contains newline characters using PowerShell backtick escaping - i.e: </p>
  
  <pre><code>`r`n
  </code></pre>
  
  <p>For demonstration purposes, the input string is then written to the console, and also dumped to a file.</p>
  
  <p>The issue I have is that when the script is run from cmd.exe using the syntax </p>
  
  <pre><code>powershell.exe script.ps1 "TEST`r`nTEST"
  </code></pre>
  
  <p>The newline characters in the string are not treated as newline, and are included literally in both the console output and the output text file.</p>
  
  <pre><code>TEST`r`nTEST
  </code></pre>
  
  <p>However, if I run this from a PowerShell environment, I get the expected result (i.e. the newline characters are parsed correctly, and a newline is inserted in the appropriate location).</p>
  
  <pre><code>TEST
  TEST
  </code></pre>
  
  <p>Similarly, if I pass in <code>\r\n</code> instead of the escaped newline characters through Windows cmd.exe, and do a <code>.replace</code> in the PowerShell script </p>
  
  <pre><code>$date = $data.replace("\r\n","`r`n")
  </code></pre>
  
  <p>I get the expected output:</p>
  
  <pre><code>TEST
  TEST
  </code></pre>
  
  <p>Is anyone able to shed some light on why this happens?</p>
  
  <p>The test script is as follows:</p>
  
  <pre><code>param([string]$data) # data to send
  
  Write-Host $data
  [IO.File]::WriteAllText('d:\temp.txt', $data)
  return 0
  </code></pre>
  
  <p>And the file is called from the command line as:</p>
  
  <pre><code>powershell.exe script.ps1 "TEST`r`nTEST"
  </code></pre>
  
  <p>The script is running on Windows Server 2012 R2, using PowerShell v4.0</p>