PowerShell: How to Use Echo with New Line


In PowerShell, echo is an alias for Write-Output and allows you to write strings to the console.

If you would like to insert a new line when using echo, you can use `n to do so.

For example, you can use the following syntax to write “hello everyone” with each word occurring on a new line:

echo "hello `neveryone"

You can use `n as many times as you’d like to insert as many new lines in the output as you’d like.

Note: The syntax `n uses a backtick, not a single quote. This is a common mistake to avoid.

The following example shows how to use this syntax in practice.

Example: How to Use Echo with New Line in PowerShell

Suppose we use echo to write “hello everyone” to the console in PowerShell:

By default, the entire string appears on one line.

However, we can use the following syntax to write “hello everyone” on two separate lines in PowerShell:

echo "hello `neveryone"

The following screenshot shows how to use this syntax in practice:

PowerShell echo new line

Notice that “hello” and “everyone” appear on two lines.

Also note that you can use `n as many times as you’d like.

For example, we can use the following syntax to the names of five different basketball teams on five different lines:

echo "Mavs `nNets `nHawks `nKings `nLakers"

The following screenshot shows how to use this syntax in practice:

Since we used `n before each new team name, each team name appeared on a new line.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Return Multiple Values from Function
PowerShell: How to Replace Multiple Strings in File
PowerShell: How to Replace Special Characters in String
PowerShell: How to Replace Text in String

Leave a Reply