61

Is it possible to write a newline to the console in PowerShell?

I tried echo "\n", but it is not translated to a new line. It just outputs \n.

1
  • If the data is coming from a .php file then echo "\n"; is the way to do it. Commented Jun 13, 2020 at 18:30

4 Answers 4

87

echo is used in PowerShell all the time. It is an alias for Write-Output.

The issue here is that you need to be using the PowerShell escape character which is a backtick. You can read more about this on TechNet on about_escape_characters.

The following special characters are recognized by Windows PowerShell:

`0 Null `a Alert `b Backspace `f Form feed `n New line `r Carriage return `t Horizontal tab `v Vertical tab 

So, if you are just trying to break up the output, you can simply use:

echo "`n" 

That will actually output two new lines as all strings sent to Write-Output (see Get-Alias echo) will be terminated with a new line regardless. Since strings are evaluated as expressions in PowerShell "" would also work but it would only output the one line.

Also, since this data is being sent to the standard output stream, it will be captured by variables and pipelines. Write-Host might be a better option if that is something you want to mitigate.

Sign up to request clarification or add additional context in comments.

2 Comments

So it needs " double quotes and not single quotes.
Double quotes permit expansion where as single quoted strings are interpreted verbatim . You can see more in Quoting Rules
12

Just do "". It will print a blank line.

2 Comments

echo is rarely used in PS. Get-Alias echo would beg to differ
Brilliant and cross-platform
10

You should use it like this:

PS D:\> "This is an `n example!" This is an example! 

PS

Comments

2
echo "Test `nNewLine" 

or

echo "Test " "NewLine" 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.