In PowerShell scripting, adding new lines or line breaks to strings or variables is a common requirement for formatting output or organizing data and improving readability.
Using `n for a new line is the simplest way to add a new line in PowerShell strings or variables. This special character represents a new line or line break in a string.
In this article, we will discuss different techniques to add new lines in PowerShell strings or variables.
PowerShell Tip: If you need a PowerShell carriage return, use `r. Use `n to add a PowerShell new line. For a carriage return and new line, use `r`n
Add PowerShell Newline – Using `r carriage return
Use the special character `r for carriage return in PowerShell to add line breaks in a string.
The below example demonstrates how to use `r PowerShell carriage return to insert a new line.
PS C:\>"Shell `r`nGeek"
In the above example, it adds a new line with PowerShell carriage return (`r) and gives output as given below.
Shell Geek
Do you know: How to use the cat command in Windows!
PowerShell new line in string output
Use `n to add a new line in string output in PowerShell. The below example shows how to add a new line in PowerShell using the special character `n.
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 is 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
We often prefer breaking down longer PowerShell script commands into multiline statements for better readability.
To add a new line 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 that 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 script, it breaks into multiple lines and improves the readability.
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 "}
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 the variable or newline in the string output for the variable.
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 data type and assign the value to it. To add a line break, we 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
Use `n to add new lines 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 PowerShell Write-Host
command, newline is added with `n. It displays the strings with new lines.
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, $EmployeeArrayList
variable contains lists of employees separated by a comma.
$EmployeeArrayList = 'Tom Smith','Adam Strauss','Tim Smith','Gary willy'
To print the array line by line or add a new line to the array, you can use the following PowerShell script.
$EmployeeArrayList = 'Tom Smith','Adam Strauss','Tim Smith','Gary willy' $empArray = $EmployeeArrayList | Out-String Write-Host $empArray
The $EmployeeArrayList variable contains the employee’s names separated by a comma. It then pipes as output to the Out-String and stores the result in a temporary $empArray
variable.
The PowerShell Write-host cmdlet prints the array on the console.
The output of the above script in PowerShell adds new lines in Array.
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.
Adding new lines to strings or variables in PowerShell is important for formatting output and organizing data effectively.
You can also create a new line break in PowerShell while defining a string.
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.
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
Thanks, Matt! Corrected now.