How to Add a New Line in a PowerShell String

There are several ways to go about inserting a line break in a PowerShell string. Here are three methods.

Brien Posey

August 2, 2022

4 Min Read
How to Add a New Line in a PowerShell String

When authoring a PowerShell script, it is often necessary to format string output so that it displays correctly on the screen. Most of the time, such formatting operations are straightforward, but things can become a bit tricky if you need to insert a line break.

In this article, I will show you how to force a new line in a PowerShell string.

If you need to display a script’s output on multiple lines, the easiest way is to simply use a separate Write-Host command for each line of output. For example, the following lines of code produce the output shown in Figure 1.

Write-Host “Line 1”Write-Host “Line 2”Write-Host “Line 3”

PowerShell example that shows Write-Host command method

PowerShell Lines 1_0

Figure 1. Each Write-Host statement results in a separate line of text in the output.

Use the `n Code Method

Although a series of Write-Host commands will work effectively, it isn’t practical for every situation. Oftentimes, complex PowerShell scripts display output that has been computed, rather than a literal value (as was the case in Figure 1). As such, it is sometimes necessary to insert a line break sequence into a string.

The most common technique for forcing a line break is to insert `n just before the character where you want the break to occur. Note that the ` character is the backtick, not an apostrophe. It appears on the same key as the tilde (~). The letter n in this sequence stands for “new line.”

Related:How To Use PowerShell Comments

To show you how this technique works, let’s pretend that we want to display the words “Hello World” on screen with the word “Hello” on one line and the word “World” on the next line. Just to make things interesting, let’s also pretend that, for whatever reason, we want to use a single Write-Host statement. We could accomplish these objectives with the following command:

Write-Host “Hello`nWorld”

You can see this command and its output in Figure 2.

PowerShell example that uses the ~n method

PowerShell Lines 2

Figure 2. I have inserted the line break sequence in the middle of a string.

Use the ForEach Command

Keep in mind that using the `n code is not the only way to force a line break. Another method you can sometimes use (it depends on the type of output that needs to be displayed) involves using a ForEach loop. This method works best when multiple pieces of data are tied to a single variable.

Here’s an example. Imagine that the text strings “Line 1,” Line 2,” and “Line 3” were assigned to a variable named $A through a line of code like this one:

$A = ‘Line 1’, ‘Line 2’, ‘Line 3’

If you want to display all three blocks of text (Line 1, Line 2, and Line 3) with each on its own line, then you could just type $A. This would tell PowerShell to display the contents of the $A variable, with each item appearing on a separate line.

Even though this approach works well, it’s not always practical. You might, for example, end up in a situation where you do not want to display every line. Similarly, you may want to append something to a line of text prior to displaying it. This is where the ForEach command comes into play.

The ForEach command allows you to deal with each piece of data (Line 1, Line 2, and Line 3, in this case) separately. And while you could use the ForEach command to simply display the various pieces of data (like in the example below), you can also use this technique to modify the individual data elements prior to displaying them.

Here is what it looks like:

$A = ‘Line 1’, ‘Line 2’, ‘Line 3’ForEach ($Item in $A) {Write-Host $Item}

Here, we are telling PowerShell that for every item ($Item) in the list ($A), we want to use the Write-Host cmdlet to display the item. Even though Write-Host is the only cmdlet enclosed in brackets, we could write an entire series of cmdlets, or even call a function. That would allow us to take any necessary action on an individual data item prior to displaying it. As it is, however, this script simply displays each item on its own line. See Figure 3 for the script and its output.

PowerShell example that shows ForEach command method

PowerShell Lines 3

Figure 3. The ForEach command is used to produce a separate Write-Host statement for each item that is stored in the $A variable.

About the Author(s)

Brien Posey

Brien Posey is a bestselling technology author, a speaker, and a 20X Microsoft MVP. In addition to his ongoing work in IT, Posey has spent the last several years training as a commercial astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space.

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like