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

Photo of author

By Victor Ashiedu

Published

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

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

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

Parameters Of PowerShell Write-Host

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

S/NParameter NameWrite-Host Parameter Meaning/Notes
1ObjectYou 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.
2NoNewlineBy 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.
3SeparatorIf you want to insert a separator string between objects displayed by the host, use the Separator parameter to specify the separator string to insert.
4ForegroundColor 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
5BackgroundColorYou 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.
6CommonParametersWrite-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.

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.

1. Use 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

2. Add New Line

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…

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

4. Specify the Text Color with the ForegroundColor Parameter

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…

5. Specify the Text Background Color with the BackgroundColor Parameter

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

6. Write the Tab Character (`)

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

6. Concatenate Strings 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)"

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

8. Display Formatted Output

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 | ForEach-Object {Write-Host $_}

And now everything is displayed nicely as expected!

9. Write Current Time

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

The simple command that writes the 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 the 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

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

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 and Write-Host (with examples), go to the How To Use PowerShell Write-Host vs Write-Output (link opens in a new browser tab) section of this guide.

2. What is the Write-Host cmdlet used for?

The Write-Host cmdlet is used to write objects to the 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 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 PowerShell.

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.

Conclusion

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, but I also discussed multiple examples and applications of the Write-Host cmdlet.

I hope you found this article helpful. If you did, click on “Yes” beside the “Was this page helpful” question below.

You may also express your thoughts and opinions by using the “Leave a Comment” form at the bottom 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.

About the Author

Photo of author

Victor Ashiedu

Victor is the founder of InfoPress Media, publishers of Ilifeguides and Itechguides. With 20+ years of experience in IT infrastructure, his expertise spans Windows, Linux, and DevOps. Explore his contributions on Itechguides.com for insightful how-to guides and product reviews.

Related Articles

Get in Touch

We're committed to writing accurate content that informs and educates. To learn more, read our Content Writing Policy, Content Review Policy, Anti-plagiarism Policy, and About Us.

However, if this content does not meet your expectations, kindly reach out to us through one of the following means:

  1. Respond to "Was this page helpful?" above
  2. Leave a comment with the "Leave a Comment" form below
  3. Email us at [email protected] or via the Contact Us page.

Leave a comment