Displaying information with Write-Host and Write-Output
Table of Contents
I. Introduction
In this chapter, we'll take a look at two cmdlets that are essential for outputting to the user console: "Write-Host" and "Write-Ouput". We'll also take a look at "Write-Warning" and "Write-Error".
When a script is executed, it's essential to have console feedback, so asnot to be "blinded" during execution, in addition to any logs the script may generate. So, if an administrator runs a script from a PowerShell console, he or she can follow what's happening in real time: is everything running smoothly? Are there any errors? What is the script currently doing? Console feedback can be used to answer a variety of questions.
The "Write-Host" and "Write-Output" PowerShell cmdlets meet exactly this need, since they can be used to display information in the console for the user.
II. Getting started with Write-Host
The "Write-Host" cmdlet lets you write a message to the user directly in the PowerShell console. Although it incorporates a set of parameters, we can use it very simply by indicating the name of the cmdlet followed by the desired message.
Here's the most basic use of Write-Host:
Write-Host "<Message à afficher>"
Write-Host "Bonjour !"
When we execute this line of code in PowerShell, it displays "Hello!" in the console.
The "Write-Host" cmdlet includes two very interesting parameters for formatting text:
- -ForegroundColor to change the text color
- -BackgroundColor to change the text background color
You must choose a color from the list proposed by PowerShell. You cannot define a color from HTML code.
Here are the available values:

If we wish to return a message to the console indicating that an operation has been successfully completed, we can use the color green via the "Green" value. Which gives:
Write-Host "Opération réussie !" -ForegroundColor Green
We could also change the background color to white, for example.
Write-Host "Opération réussie !" -ForegroundColor Green -BackgroundColor White
In the PowerShell console, we can see the difference. This is interesting, as the colors can be used to highlight certain messages.

You should also be aware that each "Write-Host" command will write its message on a new line. It is not necessary to include an empty "Write-Host" for this purpose.
In some cases, it may be useful to include a line break in the same message. To do this, use the following character: `n. It's an inverted apostrophe followed by an n.
Here's an example of how to write "Operation successful!" on a first line and then "Moving on" on a second line. There's no need to add a space between "`n" and the rest of the text, otherwise it will actually add a space to the output.
Write-Host "Opération réussie ! `nPassons à la suite" -ForegroundColor Green -BackgroundColor White
III. Getting started with Write-Output
We now turn to the"Write-Output" cmdlet, which also displays a message to the user in the console. However, it is different from"Write-Host".
Here's how to use "Write-Output":
Write-Output "<Message à afficher>"
Write-Output "Bonjour !"
We can see that the logic is the same as with Write-Host. However, this cmdlet doesn't allow you to change the formatting of the text: you can't change the color! This is the first difference, but it's not the only one.
IV. Write-Host VS Write-Output
I'm sure you prefer "Write-Host" to "Write-Output", as it allows you to adapt the colors, and therefore format the output. However, a major difference between "Write-Output" and "Write-Host" could change your mind. In addition, you should know that PowerShell best practice recommends using "Write-Output" rather than "Write-Host".
The sole purpose of the "Write-Host" cmdlet is to write text directly to the console. The "Write-Output" cmdlet not only writes text to the console, but also sends it as an object to the pipeline, making it accessible to another command.
Let's look at this difference in practice with a first example.
We'll use the"Out-File" cmdlet to write the output to a text file named "message.txt", in addition to writing it to the console. With "Write-Host", this gives this command:
"Bonjour !" | Write-Host | Out-File "C:TEMPmessage.txt"
The message is returned to the console and the file "message.txt" is created. However, the file is empty, as "Write-Host" is unable to send the object via the pipeline to the "Out-File" command.
If we simply replace "Write-Host" with "Write-Output", we get a different result: the message is returned to the console and, in addition, it's written to the "message.txt" file.
"Bonjour !" | Write-Output | Out-File "C:TEMPmessage.txt"
Let's look at a second example to see how these cmdlets behave differently.
We'll execute the "Get-LocalUser" command and display the output in the console using "Write-Host". The result will also be stored in the "$UserList" variable.
$ListeUtilisateurs = Get-LocalUser | Write-Host
We get a return to the console, but the "$UserList" variable is empty.

The same exercise with "Write-Output" will give a different result: no message in the console, but the "$UserList" variable is not empty.

V. Warning and error messages
The "Write-Warning" and"Write-Error" cmdlets are used to display warning and error messages to the user, directly in the console.
The "Write-Warning" cmdlet is used to display a warning message to the user, which can be useful for alerting the user that something unusual has occurred, but that the script can continue to run. The special feature of this message is that it will be colored yellow and preceded by the text "WARNING".
Here's an example of Write-Warning in action:
Write-Warning "Ceci est un avertissement"
AVERTISSEMENT : Ceci est un avertissement
The"Write-Error" cmdlet is used to display an error message to the user. It adds the generated error to the"$Error " variable, which contains an array of recent errors.
Here's an example of how to use "Write-Error":
Write-Error "Ceci est un message d'erreur"
Write-Error "Ceci est un message d'erreur" : Ceci est un message d'erreur
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException
Depending on the severity of the message, "Write-Warning" or "Write-Error" should be used. These two cmdlets are as easy to use as "Write-Host" and simply return to the console (except "Write-Error", which feeds the "$Error" variable).
VI. Conclusion
Now that you've read this chapter, you'll be able to return to the console within your PowerShell script, thanks to the use of 4 different cmdlets. You can choose to use "Write-Output" only, if you don't want to format the output.