Print Blank Line in PowerShell

Print Blank Line in PowerShell

Print Blank Line in PowerShell

We have two string-type messages that we want to print on the PowerShell console, but in between, we also want to have a blank line to increase the readability. For instance, we want to get results as follows:

To do as demonstrated above, we can use different ways. Let’s learn each of them below.

Use Write-Host Cmdlet

Use the Write-Host cmdlet to print blank line in PowerShell.

First, we created $string1 and $string2 variables containing string type values. Then, we used the Write-Host cmdlet to print $string1, a blank line using an empty string (""), and $string2. The Write-Host cmdlet prints customized output to the host; here, host means your PowerShell console. So, we used this advantage and empty string ("") with this cmdlet to print the blank line in PowerShell.

We have to use three Write-Host cmdlets in the above-presented solution. But, suppose you have to meet the requirements using two Write-Host cmdlets. This is where the following solution would be helpful.

Use Backtick to Escape Newline Character

Use the backtick character to escape a newline character to print a blank line in PowerShell.

Here, we used backtick to print blank line in PowerShell.

The Write-Host cmdlet uses the ToString() method to write an output on the PowerShell Console.

The main aim of this cmdlet is to assist with display-only output, which means we can use this to print a blank line, coloured text, etc. We can specify text’s colours using -ForegroundColor or -BackgroundColor parameters. We can also use the -Separator parameter to separate the displayed things. These specific results are based on the program where the PowerShell is being hosted. Let’s use the following example to understand.

OUTPUT:
powershell print blank line - background and foreground color

Like previous examples, we defined and initialized the $string1 and $string2 variables. Next, we created an array using the array operator (@()), which contained $string1 and $string2 elements. Then, we used Write-Host to print on the PowerShell console.

Note that we used one Write-Host, so we would not automatically be on a new line. This is why we used two `n; the first to move to the next line and the second to add a blank line. Further, -ForegroundColor and -BackgroundColor were used to set the foreground and background colours, as seen in the above output.

That’s all about how to print blank line in PowerShell.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *