4 Cmdlets To Display Text on the PowerShell Console Screen

Learn about PowerShell cmdlets for displaying text on the console screen: Write-Warning, Write-Error, Write-Output, and Write-Host.

Brien Posey

November 29, 2023

4 Min Read
Person typing on a keyboard and pressing Enter key
Alamy

PowerShell provides several cmdlets for displaying text on the console screen. As you may have guessed, each cmdlet is intended for a specific purpose. In this article, I will introduce four of these cmdlets and demonstrate how to use them.

Write-Warning Cmdlet

The first cmdlet is Write-Warning, which is useful when you need to display a warning message to whoever is running the script.

Creating a warning message couldn’t be simpler. Just type the command Write-Warning, followed by the text you want to display within the warning message. For example:

Write-Warning “This is an example warning.”

PowerShell screenshot showing Write-Warning cmdlet used to display a custom warning

Console Output 1

Figure 1. You can display custom warning messages by using the Write-Warning cmdlet.

Interestingly, the Write-Warning cmdlet can do more than display static messages. You can also prompt the user about whether they want to continue with script execution. This is particularly useful when a potentially serious warning is triggered. To prompt the user for a confirmation, append -WarningAction Inquire to the command. Here is an example:

Write-Warning “This is an example warning.” -WarningAction Inquire

PowerShell screenshot demonstrates the use of -Warning Action Inquire

Console Output 2

Figure 2. You can allow the user an opportunity to suspend the script following a serious warning.

Write-Error Cmdlet

Similar to generating custom warning messages, you can also create custom error messages. The cmdlet used for doing so is Write-Error. Additionally, there’s a separate cmdlet called Throw that allows you to generate an error.

At its most basic, the Write-Error cmdlet works just like the Write-Warning cmdlet. You simply enter the cmdlet followed by the error message you want to display. However, the output can sometimes be a bit confusing, as shown in Figure 3.

PowerShell screenshot shows Write-Error cmdlet used to display a custom error message

Console Output 3

Figure 3. You can use the Write-Error cmdlet to display a custom error message.

You can add context to your custom error messages by assigning a category to the error message. Append the -Category parameter, followed by the desired category name. For example:

Write-Error “This is an example error.” -Category InvalidOperation

You can see an example of how an error category works in Figure 4. Microsoft provides a list of all the available categories.

PowerShell screenshot demonstrates error category

Console Output 4

Figure 4. You can append a category to an error message.

Write-Output Cmdlet

The Write-Output cmdlet is similar to the Write-Host cmdlet, but with two notable differences.

Firstly, Write-Output exclusively supports plain text. You can’t change the text color or add other effects to the text.

The second difference is the automatic enumeration of objects by the Write-Object cmdlet. However, if you want to treat an array as a single object within pipeline operations, you can use the -NoEnumerate parameter.

Write-Output ‘A’,’B’,’C’ -NoEnumerate | Measure-Object

Granted, this is a bit of a nonsense example, but it illustrates the idea that the output is treated as an object.

Such an operation would not be allowed with the Write-Host cmdlet.

Admittedly, I can’t seem to recall ever having used the Write-Output cmdlet in a script. While I am sure that the cmdlet has its place, I can’t think of anything that Write-Output can do that can’t be done using Write-Host and variables.

PowerShell screenshot displays the use of NoEnumerate switch

Console Output 5

Figure 5. The NoEnumerate switch allows a collection to be treated as a single item.

Write-Host Cmdlet

The Write-Host cmdlet is the standard command used to display text on the console screen in PowerShell. To use this cmdlet, you simply append the text you want to display. For example:

Write-Host “This text will be displayed on the screen.”

Unlike the Write-Output cmdlet, the Write-Host cmdlet allows you to change both the foreground and the background color of the text. Here’s an example:

Write-Host “White text on a blue background” -Foregroundcolor White -BackgroundColor Blue

PowerShell screenshot shows Write-Host cmdlet supporting multiple colors

Console Output 6

Figure 6. The Write-Host cmdlet supports colorful output.

Unfortunately, Write-Host does not support other text effects such as bold or blinking text. While it was once possible to achieve such effects by embedding various ANSI escape sequences, Microsoft appears to have disabled support for ANSI-based text effects within the PowerShell console. Some ANSI escape sequences, like those for creating a new line or inserting a tab, still work, but those related to text color and attributes (bold, underline, etc.) no longer function.

About the Author(s)

Brien Posey

Brien Posey is a bestselling technology author, a speaker, and a 20X Microsoft MVP. In addition to his ongoing work in IT, Posey has spent the last several years training as a commercial astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space.

http://brienposey.com/

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like