2

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

output is:

bc

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

THanks

3 Answers 3

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"
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

-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.

1 Comment

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++.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.