Powershell

How do I Add a Newline to Command Output in PowerShell?

The new line to a command output in PowerShell can be added using the escape characters. One of the escape characters is the new line parameter “`n”. It adds a line break or inserts a new line in the PowerShell output. It is useful when the user needs to display multiple lines of code in the output of PowerShell. Further, the other commands carriage return and the “Write-Host” cmdlet can also be utilized for the same stated cause.

This tutorial will help the user to add a new line in PowerShell’s command output.

How to Add a Newline to the Output of Command in PowerShell?

These approaches can be utilized to add a new line in PowerShell:

Method 1: Use the “New line(`n)” Method to Add a New Line in PowerShell

Most usual way to add a new line to the PowerShell command output is by using the “`n”. For that reason, add it before the line you want to break or add a new line.

Example 1: Add a New Line

This example will add a new line in the output using the “`n” parameter:

write-host "Linux `nHint"

Example 2: Add a Two-Line Break

This demonstration will add a two-line break by using the “`n” parameters two times consecutively:

write-host "This will add two `n`nline breaks."

Method 2: Use the “Carriage Return(`r)” Method to Add a New Line in PowerShell

The carriage return “`r” cmdlet when used with “`n” cmdlet can also add the new line. The carriage return “`r” helps move the cursor at the start of the line after breaking it.

Example

In this illustration the “`r” will be used along with the “`n” parameter to add a new line in the output:

write-host "It is a Linux `r`n Hint `r`nPortal."

Method 3: Use the “Write-Host” Method to Add a New Line in PowerShell

Another method that is used to add single or multiple lines in the PowerShell output is by using the “Write-Host” cmdlet. It is also utilized to display multiple lines of code in the console.

Example

This illustration will print multiple new lines in the PowerShell output console using the “Write-Output”:

write-host "This is a cat."

write-host "It is a Linux OS."

write-host "It is a Linux Hint portal."

According to the above code, three “write-host” cmdlets have been added with some text inside.

That was all about adding a new line to command output in PowerShell.

Conclusion

The new line in the command output in PowerShell can be added by utilizing several methods. These methods include new line “`n”, carriage return “`r”, or the “Write-Host” cmdlet. This post has elaborated several methods with examples to add the new line to command output in the PowerShell console.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.