Reputation: 2841
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?
Upvotes: 6
Views: 4626
Reputation: 1480
Here is another way, especially if you don't want to alter the here-string itself. This quick solution works great for me as it reinstates the expected behaviour of New-Line character (CRLF) stored within a Here-String / Verbatim-String without having to alter the Here-string itself. What you can do is either:
$here_str = $here_str -split ([char]13+[char]10)
OR
$here_str = $here_str -split [Environment]::NewLine
To test, you can do a line-count:
($here_str).Count
Here is your example:
$string = @'
Line 1
Line 3
'@
#Line-Count *Before*:
$string.Count #1
$string = $string -split [Environment]::NewLine
#Line-Count *After*:
$string.Count #3
$string
Output:
Line 1
Line 3
HTH
Upvotes: 0
Reputation: 1
another option is to use:
"@+[environment]::NewLine+[environment]::NewLine+@"
which may look ugly but works as needed.
The upper example would then be:
$string = @"
Line 1
"@+[environment]::NewLine+[environment]::NewLine+@"
Line 3
"@
Upvotes: 0
Reputation: 60910
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
"@
Upvotes: 7