derek
derek

Reputation: 10227

powershell cannot write newline character

"b`nc" | out-file "a.txt"

output is:

bc

What is the correct way to write a string containing a newline character.

THanks

Upvotes: 2

Views: 723

Answers (3)

Esperento57
Esperento57

Reputation: 17492

Windows system use carriage return + endline (\r\n) like this :

"b`r`nc" | out-file "c:\temp\a.txt"

if you want Something which work on multi-plateform try this:

'b' + [environment]::NewLine + 'c' | out-file "c:\temp\a.txt"

Upvotes: 4

twglomski
twglomski

Reputation: 96

If you open with Notepad++, you'll see the line break. Also, if you run

Get-Content a.txt

You'll see the line break there too.

Issue here is encoding. For this to work:

"b`nc" | out-file a.txt -Encoding ascii

That should open properly in notepad.

Upvotes: -2

Matt
Matt

Reputation: 1240

You will occasionally need to use a 'carriage return' in conjunction with newline to get a new line to appear. Try this:

"b`r`nc" | out-file "a.txt" 

For more information on carriage returns, you can read this on Stack Overflow.

Upvotes: 3

Related Questions