Understanding When & When Not to Create PowerShell New Lines

Bill Kindle

Read more posts by this author.

Double quotes, backticks, line breaks, Oh My! Death to horizontal scroll! How do you split a long PowerShell command into multiple lines for readability? Learn how to create a PowerShell new lines easily!

A carriage return alone doesn’t always work. There are a couple of techniques to insert a line break using double quotes, backticks, and natural line continuation. In this tutorial, you will learn about all the ways to create multiple lines in PowerShell.

By the end of this tutorial, you will have learned about all the ways to create PowerShell new lines. Ready? Let’s get started!

Prerequisites

To follow along, be sure you have a Windows PowerShell or PowerShell Core terminal session open. The examples in this article will use PowerShell 7.1.3.

Breaking Lines With the Backtick

The dreaded backtick ( ` ), backquote, or grave accent, has been known to cause problems for many PowerShell users, beginners, and experts alike. And here you will see why.

Below is an example of using a backtick to create line continuation to break up a command, putting the parameters on a new line:

Get-Service `
-ServiceName BITS

When the command runs, it completes and returns the status for the BITS service:

With the backtick, a new line is formed, and the parameters show up on a new line
With the backtick, a new line is formed, and the parameters show up on a new line

The backtick, followed by a carriage return, instructs PowerShell to expect more typing to complete a command:

Get-Service # You press the return key here without adding the backtick
-ServiceName BITS # You won't be able to type this

PowerShell will execute the command on the first line without the backtick, not even presenting you with a second line. Get-Service will list all services instead of listing only the BITS service.

The below example shows what happens without the backtick:

No backtick, no second line, no chance to tell PowerShell to list only the BITS service
No backtick, no second line, no chance to tell PowerShell to list only the BITS service

Using backticks is generally frowned upon in the PowerShell community and for good reasons. Backticks are easy to miss and make your code harder to read, thus complicating debugging. If you find yourself with lots of backticks, you may want to consider splatting instead.

Continuing Lines With Double Quotes

Double quotes ( ” ” ) are often used when you need a string in PowerShell. They also can be used for line continuation when using strings.

Below is an example of using double quotes:

Write-Host "
This is a new line"

Here’s what the code looks like when run in a PowerShell terminal session:

Creating new lines with double quotes
Creating new lines with double quotes

Optimizing for Readability with `n (New Line)

Double quotes work well at the command line but can cause readability problems in scripts. Always remember: optimize your scripts for readability, especially if you want other people benefiting from the scripts you create. Special characters come into play whenever you want readability.

Both Windows PowerShell and PowerShell support special characters. There are three of them for creating a new line, carriage return, or vertical tab.

The PowerShell new line special character will create an immediate line break following the last character entered, including spaces.

For example, you want to make a string appear as two separate lines. Using the code below, you break to a new line after the word “bunch” by inserting (`n):

Write-Host "I've got a loveley bunch`nof coconuts. Here they are standing in a row."

The result looks like this:

Creating a new line with (`n)
Creating a new line with (`n)

It’s hard to spot the new line special character in the PowerShell terminal above. Because it is hard to spot, you won’t see this used a lot in the PowerShell terminal.

Breaking Lines with `r (Carriage Return)

Continuing with the previous section example, let’s look at the same code, but with a carriage return special character this time.

Copy the code below, which contains a carriage return special character after ‘active.’ to break up the message and press Enter:

Write-Host "SSH server is active.`rNo vendor support required."

Below is a screenshot of the result of running the code above.

See how tricky `r can be? It makes part of the message disappear.
See how tricky `r can be? It makes part of the message disappear.

If you expected the whole message to appear, you would notice a problem. Part of the message is missing! The carriage return special character overwrites previous characters in a string and replaces those with those that come after.

In other words, a carriage return special character does not create a new line; it eliminates them!

Creating Tabs with `v (Vertical Tab)

The vertical tab special character will create a new line. Using the same code as the last section, replace the carriage return special character with the vertical tab special character and press Enter:

Write-Host "SSH server is active.`vNo vendor support required."

Below is a screenshot of what the message looks like now in Windows Terminal:

Windows Terminal treating vertical tab as a new line.
Windows Terminal treating vertical tab as a new line.

The vertical tab character will behave differently in Windows compared to Linux. You will get different results depending on the environment you are using the character in.

The Windows Terminal will display vertical tabs as new lines, thus looking like newline output.

Creating PowerShell New Lines With the Pipeline Operator

One of the most often used natural line continuators is the pipeline operator. The pipeline operator passes objects from one command to another. A pipeline operator also breaks up lengthy commands into multiple lines.

Below is a sample of natural line continuation using the pipeline operator:

Get-Process -ProcessName 'cron' |
	Select-Object -Property Name,Id,HasExited

Below is the pipeline as a line continuation in action:

The command, using a pipe for line continuation
The command, using a pipe for line continuation

Now compare to the one below. Below is the same code without the carriage return after the pipeline. See how the pipe makes it more readable in a PowerShell terminal session,

Same as the previous one, without the pipe
Same as the previous one, without the pipe

The difference is subtle at first, but imagine a long line of code. Below is an example from Windows PowerShell and one such lengthy command:

Get-Process | Select-Object -First 25 | Where-Object -Property ProcessName -EQ "conhost" | Select-Object -Property Id,ProcessName | Where-Object -Property Handles -lt 130 | Format-Table -AutoSize
A long command, without line continuation
A long command, without line continuation

Now, look at the same code as before, but now with the pipeline operator for line continuation:

Get-Process |
	Select-Object -First 25 |
		Where-Object -Property ProcessName -EQ "conhost" |
			Select-Object -Property Id,ProcessName |
				Where-Object -Property Handles -lt 130 |
					Format-Table -AutoSize
The same as before, but using pipe for line continuation
The same as before, but using pipe for line continuation

The previous example is easier to read and more structured than the one with the line continuation.

Conclusion

As you have learned in this article, there are many ways to create line breaks and multiple lines in a way that does not break your code. You learned about how a PowerShell new line can increase readability.

Using natural line continuators actually makes your code easier to read. Line continuation breaks up the long lines that can scroll horizontally. While the pipeline operator is the most commonly used line continuator, there are 17 others line continuators waiting to be explored. Go forth, and take your newly found knowledge of PowerShell new line techniques to super-charge your PowerShell scripting!

Subscribe to Stay in Touch

Never miss out on your favorite ATA posts and our latest announcements!

Looks like you're offline!