Preserve newlines when concatenating or using heredoc in PowerShell

powershell

Solution

A simple way to do what you want is:

wrapit((Get-Content c:\temp\temp.txt | out-string))

Now the explanation: Here-strings @"" just behave like strings "" and the result is due to the PowerShell behaviour in variables expansion. Just try:

$a = Get-Content c:\temp\temp.txt
"$a"

Regarding your comment:

$a | Get-Member
TypeName: System.String
...

But

Get-Member -InputObject $a
TypeName: System.Object[]
...

The first answer is OK (it receives strings). It just does not repeat System.string each time. In the second it receive an array as parameter.

Problem

Suppose I have the a document `c:\temp\temp.txt` with contents ``` line 1 line 2 ``` and I create the following function ``` PS>function wrapit($text) { @" ---Start--- $text ---End--- "@ } ``` Then running `PS> wrapit((Get-Content c:\temp\temp.txt))` will output ``` ---Start--- line 1 line 2 ---End--- ``` How do I preserve newlines? Appending versus interpolating doesn't help. I found this related question, but here they are using a string array. I am using a single string which has newline characters in it (you can see them if you output the string directly from inside the function without concatenating and `$text | gm` confirms I'm working with a string, not an array). I can do all the string parsing in the world to hammer it into place, but that seems like I'd be banging a square peg in a round hole. What is the concept that I'm missing?

Original source

Related problems