PowerShell: How to Use Write-Host to Display Multiple Lines


You can use the Write-Host cmdlet in PowerShell to output text to the console.

To display multiple lines when using Write-Host, you can use the following methods:

Method 1: Use the @ Symbol

Write-Host @"
This text will
be displayed on
multiple lines
"@

Method 2: Use an Array

'first line', 'next line', 'last line' | Write-Host

Both of these methods produce the same result.

The following examples show how to use each method in practice.

Example 1: Use @ Symbol with Write-Host to Display Multiple Lines

Suppose that you would like to use the Write-Host cmdlet in PowerShell to display text across three lines.

We can use the following syntax to do so:

Write-Host @"
This text will
be displayed on
multiple lines
"@

The following screenshot shows how to use this syntax in practice:

PowerShell Write-Host multiple lines

Notice that the output displays the text across three lines, just as we specified.

Example 2: Use Array with Write-Host to Display Multiple Lines

Another way to use the Write-Host cmdlet in PowerShell to display text across multiple lines is to simply declare each line as an element in an array and then pipe the array to the Write-Host cmdlet.

We can use the following syntax to do so:

'first line', 'next line', 'last line' | Write-Host

The following screenshot shows how to use this syntax in practice:

PowerShell Write-Host with multiple lines using an array

The output displays the text across three lines, just as we specified.

Also notice that this produces the same result as the previous example. Feel free to use whichever method you prefer.

Note: You can find the complete documentation for the Write-Host cmdlet in PowerShell here.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Use Write-Host with Specific Colors
PowerShell: How to Use Write-Host and Display Tab Character
PowerShell: How to Replace Text in String

Leave a Reply