PowerShell Newline

Sometimes when you work on PowerShell this question arrives, “how can I use Windows PowerShell to add a new line between lines for my text output?”

The answer is simple you have to use `n (backtick with letter n) this will add Line break or Newline in PowerShell.

PowerShell Newline in String Output

Below example, show how to use “`n” to add a new line in string output.

Example

PS C :\> Write-Host "This text have one newline `nin it."

This above command writes the text on the console with a new line. See below output screen –

Powershell-newline

And if you need a carriage return (CR) use `r(backtick with letter r). For a carriage return and a newline `r`n together.

What is Carriage Return?

The term carriage return comes from printer terminology; it moves the cursor to the beginning of the current line. To read more about carriage return click here.

So, try below command to add a new line in PowerShell with a carriage return.

PS C :\> Write-Host "This text have one newline `r`nin it."

PowerShell Newline in Command

Suppose we have one-liner long command, which you need to break into multiple lines so that it will become readable and you do not need to scroll the PowerShell editor at the end to see it.

If I take an example, suppose you need below basic information from your system –

  • OS version
  • Install date
  • Service pack version
  • OS architecture
  • Boot device
  • Build number
  • Hostname

To get this information you need to execute below PowerShell command –

Get-CimInstance Win32_OperatingSystem | Select-Object  Caption, InstallDate, ServicePackMajorVersion, OSArchitecture, BootDevice,  BuildNumber, CSName | FL

This is long one-liner command now if we need to break that command into multiple lines the use “` (Back tick symbol)” like below –

Get-CimInstance Win32_OperatingSystem `
| Select-Object Caption, InstallDate,ServicePackMajorVersion, OSArchitecture, BootDevice, BuildNumber, CSName `
| FL

Notice this “`” (backtick symbol) after each line.

Powershell-newline-in-command

I hope now you don’t have any issue yo use PowerShell newline character in your script.