2

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

output is:

bc

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

THanks

derek
  • 9,358
  • 11
  • 53
  • 94

3 Answers3

4

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"
Esperento57
  • 16,521
  • 3
  • 39
  • 45
3

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.

Community
  • 1
  • 1
Matt
  • 1,230
  • 16
  • 19
-2

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.

twglomski
  • 96
  • 4
  • I tested ``"b`nc" | out-file a.txt -Encoding ascii`` and found that when opening notepad, the line break was not there. I did see it in Notepad++. – Matt Jan 24 '17 at 22:39