Table of Contents
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:
1 2 3 4 5 |
String Message 1 Goes Here. It Must Be a Blank Line String Message 2 Goes Here. |
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.
1 2 3 4 5 6 7 |
$string1 = "This is string one." $string2 = "This is string two." Write-Host $string1 Write-Host "" Write-Host $string2 |
1 2 3 4 5 |
This is string one. This is string two. |
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.
1 2 3 4 5 6 |
$string1 = "This is string one." $string2 = "This is string two." Write-Host "$string1`n" Write-Host $string2 |
1 2 3 4 5 |
This is string one. This is string two. |
Here, we used backtick to print blank line in PowerShell.
1 2 3 |
Before talking about the code, remember one thing, the Write-Host not only print the customized output but also jump to the next line. So, to have a blank line between $string1 and $string2, we used the newline character (`n) with the first Write-Host cmdlet. It means we will automatically be on newline due to using Write-Host, and `n will add one blank line. Finally, the second Write-Host cmdlet will print the value of the $string2 variable. |
The
Write-Host
cmdlet uses theToString()
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.
1 2 3 4 5 6 |
$string1 = "This is string one." $string2 = "This is string two." $array = @($string1, $string2) Write-Host $array -Separator "`n" -ForegroundColor DarkGreen -BackgroundColor White |
OUTPUT:
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.
Further reading:
That’s all about how to print blank line in PowerShell.