Home » PowerShell » PowerShell – How to add newline to string or variable?

PowerShell – How to add newline to string or variable?

When working with PowerShell, it is often necessary to add new lines in strings and variables to improve readability and to make output neat. Using Windows PowerShell new line can be easily added to text output or variables.

In this blog post, I will explain adding new line in string output or variable, and how to use PowerShell new line ` (backtick) character to add new line.

PowerShell Tip: If you need a PowerShell carriage return, use `r. Use `n to add PowerShell new line. For a carriage return and new line, use `r`n

PowerShell - add newline to string (carriage return)
add PowerShell new line to string (carriage return)

There are different ways to add a new line to a string or variable in PowerShell using carriage return `r or line continuation character ` at the end of code.

Backtick (`) character is the PowerShell line continuation character. It ensures the PowerShell script continues to a new line.

To use the line continuation character in PowerShell, type space at the end of the code, use backtick ` and press enters to continue to a new line.

Add PowerShell Newline – Using `r carriage return

Use the special character `r for carriage return in PowerShell. The below example shows how to use `r PowerShell carriage return to insert a new line.

PS C:\>"Shell `r`nGeek"

In the above example, it adds a newline with PowerShell carriage return (`r) and gives output as

Shell
Geek

Do you know: How to use the cat command in Windows!

PowerShell new line in string output

Use `n to add newline in string output in PowerShell. The below example shows how to use PowerShell new line `n in string output.

PS C:\>"Welcome to Shell Geek `nIts all about PowerShell"

In the above example, using `n in the string, insert a new line in the string output in PowerShell.

The output of the above command as below

Welcome to Shell Geek
Its all about PowerShell

Cool Tip: Tail – Get the last lines of a file in PowerShell!

Using PowerShell newline in Command

More often, we have longer PowerShell script commands and we want to beautify them using multiline statements instead of one single-line command.

To add newline in the PowerShell script command, use the` (backtick) character at the end of each line to break the command into multiple lines.

In the below example, we want to get free disk space on a local computer name is a single-line command which makes it very difficult to read and manage.

We can easily break it using ` (PowerShell backtick character) for line continuation in a given command

Get-CimInstance -ComputerName localhost win32_logicaldisk | where caption -eq "C:" | foreach-object {write " $($_.caption) $('{0:N2}' -f ($_.Size/1gb)) GB total, $('{0:N2}' -f ($_.FreeSpace/1gb)) GB free "}

After using ` (PowerShell backtick character) for a line break in the command, it can be easily read and managed.

Get-CimInstance -ComputerName localhost win32_logicaldisk `
| where caption -eq "C:" `
| foreach-object {write " $($_.caption) $('{0:N2}' `
-f ($_.Size/1gb)) GB total, $('{0:N2}' `
-f ($_.FreeSpace/1gb)) GB free "}

As seen in the above example, using the ` line continuation character in PowerShell to continue code in a new line. It makes the command more readable by splitting it across multiple lines.

Cool Tip: Use Test Connection to ping a list of computers in PowerShell!

Add PowerShell newline to Variable

Using PowerShell carriage return `r or `n, you can add PowerShell newline in variable or newline in string output for the variable as below

PS C:\>$strOutput = "Welcome you to Shell Geek `r`nIts all about PowerShell" 
PS C:\>$strOutput
Welcome you to Shell Geek
Its all about PowerShell

In the above PowerShell script to add newline to a variable, we define a variable with string text and added `n to create a new line for variable output.

When we print the strOutut variable on the terminal, it gives output with a line break in between the string output.

Cool Tip: Replace text in a string using PowerShell!

Add PowerShell new line with Write-Host

Using `n to add a newline in string output using the PowerShell Write-Host command. The below command prints a new line string on the console

PS C:\>Write-Host "Welcome you to Shell Geek `nIts all about PowerShell"
Welcome you to Shell Geek
Its all about PowerShell

In the above script, using PowerShell Write-Host newline is added with `n and prints the string on the console with a new line in the text.

Cool Tip: Do you know how to download a zip file in PowerShell?

PowerShell Add New Line to Array

To add new lines to an array in PowerShell, use the pipe operator (|) and the OUt-String cmdlet.

Let’s consider an example of an Employee array having employee names stored in an array variable as below

$EmployeeArrayList = 'Tom Smith','Adam Strauss','Tim Smith','Gary willy'

$EmployeeArrayList variable contains lists of employees separated by a comma. To print the array line by line or add new line to the array using the below PowerShell script

$EmployeeArrayList = 'Tom Smith','Adam Strauss','Tim Smith','Gary willy'

$empArray = $EmployeeArrayList  | Out-String

Write-Host $empArray

In the first command, we store the list of employees in variable $EmployeeArrayList

The second command uses an array variable and pipes its output to Out-String and stores the result in a temporary $empArray variable.

The third command uses the PowerShell Write-host cmdlet to print the array on the console.

The output of the above script in PowerShell to add new line in Array as below

PowerShell Print array multiple lines
PowerShell Print array multiple lines

Cool Tip: PowerShell String Concatenation with Examples!

Conclusion

I hope you found the above article about different ways to add PowerShell newline to variable or string output in PowerShell helpful.

Using PowerShell carriage return and PowerShell newline ` (backtick character) at the end of each line to break the command into multiple lines.

You can also create a PowerShell new line break while defining a string as below

 PS C:\> "Shell
>>Geek"
Shell
Geek

Read more here about how to create a multiline string in PowerShell!

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

2 thoughts on “PowerShell – How to add newline to string or variable?”

  1. in your employee array list output example above, I believe there is a slight error on line 3 – should it be $EmployeeArrayList instead of just $Employee

    Reply

Leave a Comment