PowerShell Write-Host Explained: Syntax, Parameters, And Examples

Are you curious to learn how to use PowerShell Write-Host to write output to a host? This article will be your guide.

This article starts with a quick overview of the Write-Host cmdlet. Then, in the second section, you’ll learn the cmdlet’s syntax.

Following that, I explain the parameters of Write-Host. Then, I have included ten examples in section four to help you learn how to use this cmdlet.

Finally, if you still have any unanswered questions, you can get answers to questions people mostly ask about PowerShell Write-Host.

PowerShell Write-Host: Overview

PowerShell Write-Host: Overview

Have you ever written a PowerShell script, and you need to display some messages on the console? The Write-Host cmdlet is your man!

You can use this simple-to-use cmdlet to display any message on the PowerShell host or console.

This cmdlet has some useful features. For example, you can specify the color of the text by using the ForegroundColor parameter.

Similarly, you can specify the background color using the BackgroundColor parameter (more on parameters in the third section).

Before I move on, I like to mention that starting in Windows PowerShell 5.0, Write-Host is a wrapper for Write-Information.

Syntax Of PowerShell Write-Host

Syntax Of PowerShell Write-Host

The Write-Host cmdlet has just one syntax. Here it is…

Write-Host
     [[-Object] <Object>]
     [-NoNewline]
     [-Separator <Object>]
     [-ForegroundColor <ConsoleColor>]
     [-BackgroundColor <ConsoleColor>]
     [<CommonParameters>]

Parameters Of PowerShell Write-Host

Parameters Of PowerShell Write-Host

I have provided a detailed explanation of the Write-Host cmdlet parameters in the table below.

S/N Parameter Name Write-Host Parameter Meaning/Notes
1 Object You use the Object parameter to specify the objects (texts) you wish to display in the host or console. This parameter is in position zero, so you do not need to specify it before adding the objects (texts) you want to be displayed on the console.
2 NoNewline By default, the string representations of the input objects are concatenated to form the output. However, when you specify the NoNewline parameter, PowerShell Write-Host will not add spaces or newlines between the output strings. Additionally, no new line is added after the last output string.
3 Separator If you want to insert a separator string between objects displayed by the host, use the Separator parameter to specify the separator string to insert.
4 ForegroundColor You use this parameter to specify the text color of the object you want to be displayed on the host. There is no default text color. You can specify the following values for the ForegroundColor parameter – Black, DarkBlue, DarkGreen, DarkCyan, DarkRed, DarkMagenta, DarkYellow, Gray, DarkGray, Blue, Green, Cyan, Red, Magenta, Yellow, and White
5 BackgroundColor You use the BackgroundColor parameter to specify the background color of the text (object) you want to display on the host. You can use the same colors listed in the ForegroundColor parameter above.
6 CommonParameters Write-Host supports PowerShell CommonParameters like ErrorAction, ErrorVariable, and the rest. To learn more about PowerShell common parameters, visit the about_CommonParameters page. Alternatively, you can run get-help about_CommonParameters command in PowerShell.

PowerShell Write-Host Examples

PowerShell Write-Host Examples

In this section, you’ll learn different ways to use the Write-Host cmdlet. I have featured ten examples and applications of Write-Host.

In my first example below, I different Write-Host and Write-Output and also offer an example.

How To Use PowerShell Write-Host vs Write-Output

The fundamental difference between Write-Host and Write-Output is how PowerShell processes the information from the cmdlets. While Write-Host writes to the console itself, Write-Output writes to the pipeline.

Why does this matter?

Well, since Write-Host does not write to the pipeline, you cannot feed its output into the next command via the pipeline. On the contrary, because Write-Output writes to the pipeline, you can use its output to the next command via the pipeline.

To make the difference clearer, I will demonstrate with the following example.

$ProcessID = (Get-Process -Name ACCSvc).Id
$ProcessName = (Get-Process -Name ACCSvc).ProcessName
$Handles = (Get-Process -Name ACCSvc).Handles

I can use the command below to write the information “The process, ACCSvc with ID 5492 has 86 handles”

Write-Host "The process, $ProcessName with ID $ProcessID has $Handles handles"

However, I will not be able to send this information to a text file by piping the output to the Add-Content command.

On the other hand, Write-Output displays the same message on the host (console). Then, pipe the result (in the same command) to Add-Content, adding the same information to a text file.

Write-Output "The process, $ProcessName with ID $ProcessID has $Handles handles" | Tee-Object -FilePath D:\report\Write-Output.txt

So, the last command displays the information on the console and sends it to a text file.

To get Write-Output to display the message on the console and send it to a text file, I piped the result of the Write-Output to the Tee-Object command.

How To Use PowerShell Write-Host vs Write-Output

How To Add New Line With PowerShell Write-Host

By default, the PowerShell Write-Host does not add a new line when it writes objects to the console. Rather, it displays everything in a single line.

Before I show you how to use Write-Host to add a new line, let me first show you a standard command.

In my last example, I used this set of commands to generate the information I needed to write to the host. Then, I used the second command to write the information to the host.

$ProcessID = (Get-Process -Name ACCSvc).Id
$ProcessName = (Get-Process -Name ACCSvc).ProcessName
$Handles = (Get-Process -Name ACCSvc).Handles
Write-Host "The process, $ProcessName with ID $ProcessID has $Handles handles"

Here is the result of the last command…

How To Add New Line With PowerShell Write-Host

Moving on to this example’s next stage, I’ll now write the following texts to the next line “Thank you for using my script.”

To achieve this, I will add the following code between the text I want to be displayed in the first line and the text I want to be displayed in the second line…

`r`n

In effect, r adds a carriage return while `n character adds a new line. However, you need both characters to add the carriage return and the new line.

Here is the final code where I use Write-Host to add a new line when I write output to host…

Write-Host "The process, $ProcessName with ID $ProcessID has $Handles handles `r`nThank you for using my script."

If you look closely, I did not add a space between `r`n characters and the text I want PowerShell Write-Host to display in the second line.

Here is the result of the command…

How To Use PowerShell Write-Host To Write Information From A Variable

To show how to use Write-Host to write information saved in a variable, I will use the following commands:

$DisplayName = (Get-Service -Name VSS).DisplayName
$Name = (Get-Service -Name VSS).Name
Write-host "The display name of $Name is $DisplayName"

The first command saves the display name of a Windows service to a variable I called DisplayName. Similarly, in the second command, I saved the name of the service in another variable – Name.

Finally, I used the Write-Host command to write the information saved in the two variables to host. Here is the result of the last command.

How To Specify The Text Color With The ForegroundColor Parameter Of PowerShell Write-Host

In this example, you’ll learn how to change the color of the text you display with Write-Host. The command has a parameter called ForegroundColor.

In this example, I want to change the color of the text in this command to Magenta.

$DisplayName = (Get-Service -Name VSS).DisplayName
$Name = (Get-Service -Name VSS).Name
Write-host "The display name of $Name is $DisplayName" -ForegroundColor Magenta

When you write a script that displays multiple information on the console, changing the text color improves the user experience. Here is the result of the command…

How To Specify The Text Background Color With The BackgroundColor Parameter Of PowerShell Write-Host

In the last example, I showed you how to change text color when you write to the host. In this example, you’ll learn how to change the background color.

Here, you need the BackgroundColor Parameter instead.

$DisplayName = (Get-Service -Name VSS).DisplayName
$Name = (Get-Service -Name VSS).Name
Write-host "The display name of $Name is $DisplayName" -BackgroundColor Magenta

Now PowerShell Write-Host used the default text color, white. Then, it changed the text background to Magenta.

How To Specify The Text Background Color With The BackgroundColor Parameter Of PowerShell Write-Host

How To Write The Tab Character (`) With PowerShell Write-Host

In PowerShell, the tab character (`) is a special character. Therefore, if you run the command write-host “`” in PowerShell, it will not display the tab character (`) as expected.

Instead, it will display >>; indicating that it was expecting additional input after the tab character.

So, how do you use Write-Host to write the tab character (`) to the PowerShell host?

One way to achieve this is to first save the tab character (`) in a variable using single quotes, like this:

$tab = '`'

Then, use Write-Host to call the variable as shown in the command below…

write-host $tab

You can also add other strings to the write-host command. Here is an example…

write-host "This is the tab character written with write-host $tab"

…and here are the results of the last two commands in PowerShell…

How To Write The Tab Character (`) With PowerShell Write-Host

How To Concatenate Strings In PowerShell When Written With Write-Host

If you have objects from a variable and wish to combine them with a string, for example, dash (-), then send it to the host with Write-Host, it gets complicated.

For example, in my previous examples, I used the following commands:

$DisplayName = (Get-Service -Name VSS).DisplayName
$Name = (Get-Service -Name VSS).Name
Write-host "The display name of $Name is $DisplayName"

The command works as expected.

But if I run a single command that saves the output of “Get-Service -Name VSS” in a variable $VSS. Then, if I use the second command below to write information to the host…

$VSS = Get-Service -Name VSS
Write-host "The display name of $VSS.Name is $VSS.DisplayName"

…this command will not display the expected information.

To solve the problem, I’ll modify the command as shown below.

Write-host "The display name of $($VSS.Name) is $($VSS.DisplayName)"

The only change I made to the last command is adding the $ character before $VSS.Name and $VSS.DisplayName

Now the command displays the information as expected.

You can even make further modifications to the information. For example, you may add another string. for example, dash (-), as shown in the command below:

Write-Host "The display name of $($VSS.Name) - $($VSS.DisplayName)"

How To Pass Variable In Array To Write-Host

It is easy to write information saved in an array to the host. All that’s required is to add the array next to Write-Host.

Here is an example…

$Array = @("System", "Input", "Output")
Write-Host $Array

In this example, the first command saves the array in a variable I call Array. Then, in the second command, write-host writes what is saved in the variable on the host.

Unfortunately, as you can see from the screenshot above, write-host writes all the array members in a single line. But what if you want them in single lines?

We can apply a simple trick to achieve this.

First, I will break down the array members into their individual members. Here are the commands that do this…

$Array[0]
$Array[2]
$Array[3]

I have another example later in this guide that explains how to break array members into individual items.

Once I have broken the array members, I’ll use introduce the characters `r`n used in a previous example. Here is the final script…

$Array = @("System", "Input", "Output")
Write-Host $Array
$array0 = $Array[0]
$array1 = $Array[1]
$array2 = $Array[2]
Write-Host $array0`r`n$array1`r`n$array2

And here you have your result…

How To Display Formatted Output With PowerShell Write-Host

In this example, I want to display the output of Get-Service on my PowerShell console. So, first, I save the output of Get-Service to the services variable.

$services = Get-Service

Then, to be sure that the table is properly formatted, I call the variable.

$services

So, everything looks good until I call the same variable in a write-host command. See the command below this screenshot.

How To Display Formatted Output With PowerShell Write-Host
Write-Host $services

Here is how Write-Host displays the information. Obviously, not desirable!

To fix the problem, use the command below:

$services | Format-Table | Out-String |% {Write-Host $_}

And now everything is displayed nicely as expected!

How To Write Current Time With PowerShell Write-Host

The command you use to write current time with Write-Host depends on how you want to format your output.

The simply command that writes current date and time to host is…

Write-Host $(Get-Date)

This command displays the current date and time…

However, if you want to display current time without the date, use the command below:

$currentTime = Get-Date -format "HH:mm:ss"
Write-Host "Backups complete at $currentTime"

The first command saves the current time in the currentTime variable. Then, the second command displays write the information in the Write-Host on the console.

How To Write Current Time With PowerShell Write-Host

How To Use Write-Host To Write Texts With Different Colors On The Same Line

The trick to writing texts with different colors on the same line is to use two Write-Host commands in the same line. However, you need to include the NoNewline parameter in the first Write-Host command.

Then, separate the two commands with semi-colon (;) character.

To demonstrate this in a real command, I will write the following texts in the colors specified.

“This color is red” – written in red
“This color is blue” – written in blue

Here is the command that does the magic!

Write-Host "This color is red" -ForegroundColor red -NoNewline; Write-Host " This color is blue" -ForegroundColor blue

Here is the result of the command in PowerShell. Did you notice that there is a space between the two texts?

How To Use Write-Host To Write Texts With Different Colors On The Same Line

I added that space by adding a space after the first quote in the second Write-Host command – ” This color is blue”.

Moving on, if you want PowerShell to move the second text to a new line, remove the NoNewline parameter in the first Write-Host command.

The command will now look like this…

Write-Host "This color is red" -ForegroundColor red; Write-Host " This color is blue" -ForegroundColor blue

And the result like this…

How To Use Write-Host To Write Texts With Different Colors On The Same Line

PowerShell Write-Host: Frequently Asked Questions

1. Should I Use Write-Output or Write-Host?

Whether you chose to use Write-Output or Write-Host depends on what you want to achieve. If you just want to write some texts on the console (host) but do not need to send the information to the pipeline, use Write-Host.

On the other hand, if you wish to write a text to the console and also send it to the pipeline, you need to use Write-Output.

For a detailed explanation of the difference between Write-Output andr Write-Host (with examples), go to How To Use PowerShell Write-Host vs Write-Output (link opens in a new browser tab) section of this guide.

2. What Is Write-Host Cmdlet Used For?

The Write-Host cmdlet is used to write objects to host (display on the PowerShell console). It is important to note that Write-Host does not generate pipeline output.

So, you cannot send the output of Write-Host to another cmdlet via the pipeline. If you need to send a text displayed on the console to the pipeline, use Write-Output instead.

3. How Do You Write To A Text File In PowerShell?

There are three cmdlets you can use to write texts to a text file in PowerShell. You can use Out-File or Add-Content.

In addition to those two, you can also use Set-Content to to write texts to a text file in PowerShell. To learn how to write texts to a text file in PowerShell, read my article How To Use PowerShell To Write To A Text File (opens in a new browser tab).

4. How Do I Write To A log File In PowerShell?

You can use three cmdlets to write to a log file. To write to a log file, you can use Set-Content or Add-Content.

Alternatively, you can use the Out-File cmdlet. You can also write errors to a log file in PowerShsll.

To write errors to a log file, use the Write-Error cmdlet.

5. What Is A Log File In PowerShell?

In PowerShell a log file is a text file with either the .log or .txt file extension. You can use a log file to write information using Set-Content, Add-Content or Out-File cmdlets.

PowerShell Write-Host: My Final Thoughts

The PowerShell Write-Host is a cmdlet you’ll likely need in your day-to-day scripting tasks. If you ever need to display information on the PowerShell console, you’ll use this cmdlet.

In this guide, I have provided a detailed explanation of the essential things you need to use Write-Host. Specifically, I covered the syntax, and the parameters of PowerShell Write-Host.

Not only that, I also discussed multiple examples and applications of the Write-Host cmdlet.

My hope is that I was able to explain this extremely useful cmdlet to you. I also hope that you found the guide easy to follow and easy to understand.

If you found this guide easy to understand and informative, kindly vote Yes to the Was this post helpful? below. On the other hand, if you prefer to share your thoughts on this article, kindly use the “LEAVE A REPLY” at the end of this page.

Finally, I strongly believe that you’ll find our other Windows PowerShell Explained articles useful as well. We also have Windows PowerShell How-To Guides, that offers you more ways to learn PowerShell.

Leave a Reply

Your email address will not be published.

You May Also Like