Home ยป How to Add New Line in PowerShell

How to Add New Line in PowerShell

In PowerShell, you can use the Write-Host cmdlet, the echo command, or the Write-Output cmdlet to add a new line.

The following methods show how you can do it with syntax.

Method 1: Using Write-Host

Write-Host "This is line 1"
Write-Host "This is line 2"

This example will print the output on a new line.

Method 2: Using the Write-Host with backticks (`)

Write-Host "This is line 1"`n"This is line 2"

This example will write the output on the new line.

Method 3: Using the Echo command

echo "This is line 1"
echo "This is line 2"

This example will echo the output on the new line.

Method 4: Using the Write-Output

Write-Output "This is line 1"
Write-Output "This is line 2"

This example will print the output on new lines.

The following examples show how you can use these methods.

How to use Write-Host Cmdlet in PowerShell to Add a New Line

The following PowerShell script shows how you can do it.

Write-Host "This is line 1"
Write-Host "This is line 2"

Output:

This is line 1
This is line 2

In this PowerShell script, the Write-Host cmdlet prints each line on a separate line.

How to Use Write-Host with Backticks in PowerShell to Add a New Line

You can use the Write-Host cmdlet with the backticks (`) to add a new line in PowerShell.

Write-Host "This is line 1"`n"This is line 2"

Output:

This is line 1
This is line 2

In the script, the Write-Host cmdlet uses the backtick (`n) character as an escape sequence within a string. It adds a new line in PowerShell.

How to Use Echo Command to Add a New Line in PowerShell

You can also use the echo command to add a new line in PowerShell.

echo "This is echo command"
echo "It is used to echo the output on the terminal"

Output:

This is echo command
It is used to echo the output on the terminal

In this script, the echo command in PowerShell is used to add a new line.

How to Use Write-Output to Add a New Line in PowerShell

Another way to add a new line in PowerShell is to use the Write-Output cmdlet.

The Write-Output is an implicit command, which means that when you output a value, it is automatically sent to the Write-Output cmdlet.

Write-Output "This is line 1"
Write-Output "This is line 2"

Output:

This is line1
This is line2

In this script, the Write-Output cmdlet prints the output in new lines.

You can also use the new line escape character ( `n) to add a new line in PowerShell.

Write-Output "This is Line1`nThis is Line2"

Output:

This is Line1
This is Line2

Conclusion

I hope the above article on adding a new line in PowerShell is helpful to you.

You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.