lapingultah
lapingultah

Reputation: 316

Powershell Here-String line breaks not working with output file

I have several hundred lines of text defined in a varibale as here-string:

$outputText = @"
Line1
Line2
Line3
And so on...
"@

If I print the $outputText on screen, it's showing properly:

PS > $outputText
Line1
Line2
Line3
And so on...
PS > 

However, when I try to output it to a file in any way I can figure, I always lose the line breaks and the file looks like this:

Line1Line2Line3And so on...

I've tried to following:

$outputText | Set-Content file.txt
Add-Content file.txt -Value $outputText
Out-File -InputObject $outputText file.txt

If I add 'r'n (with proper ticks) at the end of each line, the visible output has extra line break between each line but the output file is showing each line correctly. However, putting the return carriage and new line escapes after each of the several hundred lines is not really an option.

How would I be able to properly output the line breaks in a file easier or define the multi-line string some other way so the line breaks would work without complicated escape characters or such?

Upvotes: 2

Views: 5423

Answers (3)

Kalin Tashev
Kalin Tashev

Reputation: 21

$FilePath = YourOutputFilePath
$InputString = 'This is my text with new lines.
It would be nice to have new lines in my text file output.
Also, to preserve this formatting.'   
Out-File -FilePath $FilePath -InputObject $InputString.Replace("`n", "`r`n") -Encoding unicode -Append -Force

This worked well for me.

Upvotes: 0

js2010
js2010

Reputation: 27428

This is true with multi line strings too. Another way to do it. Turn unix text (`n) into windows text (`r`n).

$outputtext = @"
Line1
Line2
Line3
And so on...
"@ -replace "`n","`r`n"

Doublecheck it:

$outputtext -replace '\n','\n' -replace '\r','\r'

Line1\r\nLine2\r\nLine3\r\nAnd so on...

The first argument of -replace can use either the backslash or backquote notation.

Upvotes: 1

user6811411
user6811411

Reputation:

A here string puts the text in ONE string, lines delimited with a LF/0xA.

$outputText = @"
Line1
Line2
Line3
And so on...
"@
$outputText | Format-Hex

           00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

00000000   4C 69 6E 65 31 0A 4C 69 6E 65 32 0A 4C 69 6E 65  Line1.Line2.Line
00000010   33 0A 41 6E 64 20 73 6F 20 6F 6E 2E 2E 2E 0D 0A  3.And so on.....

In contrast splitting the string:

$outputText = @"
Line1
Line2
Line3
And so on...
"@ -split '\n'  # or -split "`n"

$outputText | Set-Content file1.txt
Out-File -InputObject $outputText file2.txt

1..2|%{(Get-Content ".\file$_.txt" -raw) | Format-Hex}

           00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

00000000   4C 69 6E 65 31 0D 0A 4C 69 6E 65 32 0D 0A 4C 69  Line1..Line2..Li
00000010   6E 65 33 0D 0A 41 6E 64 20 73 6F 20 6F 6E 2E 2E  ne3..And so on..
00000020   2E 0D 0A                                         ...


           00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

00000000   4C 69 6E 65 31 0D 0A 4C 69 6E 65 32 0D 0A 4C 69  Line1..Line2..Li
00000010   6E 65 33 0D 0A 41 6E 64 20 73 6F 20 6F 6E 2E 2E  ne3..And so on..
00000020   2E 0D 0A                                         ...

Other things possibly influencing the output (not here),


> $OutputEncoding


IsSingleByte      : True
BodyName          : us-ascii
EncodingName      : US-ASCII
HeaderName        : us-ascii
WebName           : us-ascii
WindowsCodePage   : 1252
IsBrowserDisplay  : False
IsBrowserSave     : False
IsMailNewsDisplay : True
IsMailNewsSave    : True
EncoderFallback   : System.Text.EncoderReplacementFallback
DecoderFallback   : System.Text.DecoderReplacementFallback
IsReadOnly        : True
CodePage          : 20127

PS: running the 2nd script in VSCode returned a 0D/0D/0A sequence, had to use -split '\r\n'

Upvotes: 7

Related Questions