Skip to content Skip to sidebar Skip to footer

Continueing a Command on Another Line Powershell

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"        

Brien Posey PowerShell example that shows Write-Host command method

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."

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.

Brien Posey PowerShell example that uses the ~n method

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.

Brien Posey PowerShell example that shows ForEach command method

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

priesttwoultaidne.blogspot.com

Source: https://www.itprotoday.com/powershell/how-add-new-line-powershell-string

Post a Comment for "Continueing a Command on Another Line Powershell"