Search code examples
powershellstring-formatting

PowerShell Here-String that keeps newlines


The PowerShell code:

$string = @'
Line 1

Line 3
'@
$string

Outputs:

Line 1
Line 3

But I want it to output:

Line 1

Line 3

How can I achieve that?


Solution

  • In ISE works fine and in script works too. I don't remember where, but I read that is a bug in the console host code and empty lines are discarded when entered interactively for here-string. At the moment I can't test if in Powershell V.3.0 console bug is fixed.

    Link to the issue: http://connect.microsoft.com/PowerShell/feedback/details/571644/a-here-string-cannot-contain-blank-line

    Workaround: add a backticks `

    $string = @"
    Line 1
    `
    Line 3
    "@