fbpx

PowerShell Write-Host Cmdlet: Tutorial

In this article, we will explore the ins and outs of Write-Host, including its syntax, common use cases, and tips for avoiding some of the most common mistakes.

Whether you’re new to PowerShell or a seasoned scripter, understanding how to use Write-Host effectively will help you become a more efficient and effective administrator.

What does the Write-Host Cmdlet do?

The Write-Host cmdlet is a PowerShell command used to display output directly in the console. When a PowerShell script or command is executed, the output is typically displayed in the console window.

But sometimes, the output can be too long or difficult to read, especially if it includes multiple data types such as strings, variables, and objects.

Write-Host allows you to display information in a more readable and organized format by specifying exactly what you want to display and how it should be formatted.

This can be helpful for debugging scripts, providing user feedback, or simply displaying information to help you better understand what is happening within your PowerShell environment.

 It is important to note, however, that using Write-Host excessively or inappropriately can lead to issues with script performance, readability, and maintainability, so it is important to use this tool judiciously and in accordance with best practices.

Message formatting with the Write-Host Cmdlet

Formatting messages for readability is an important aspect of using the Write-Host cmdlet in PowerShell. By formatting your messages properly, you can make them easier to read and understand, and also make them more visually appealing.

One common formatting option is to use different colors for different parts of your message. For example, you might want to use red text for error messages, yellow text for warning messages, and green text for success messages.

To do this, you can use the -ForegroundColor parameter with values such as Red, Yellow, or Green. For example, Write-Host “Error: Something went wrong” -ForegroundColor Red will display the message “Error: Something went wrong” in red text.

Suppose you have a PowerShell script that performs a series of checks on a computer system, and you want to display the results of each check in a clear and organized way. Here’s how you might use the Write-Host cmdlet to format your output:

# Check system memory

if ((Get-CimInstance Win32_OperatingSystem).FreePhysicalMemory -lt 1GB) {

    Write-Host "System memory check: WARNING - Low memory detected" -ForegroundColor Yellow -BackgroundColor DarkRed

} else {

    Write-Host "System memory check: OK" -ForegroundColor Green

}


# Check system disk space

if ((Get-CimInstance Win32_LogicalDisk -Filter "DeviceID='C:'").FreeSpace -lt 50GB) {

    Write-Host "System disk space check: WARNING - Low disk space detected" -ForegroundColor Yellow -BackgroundColor DarkRed

} else {

    Write-Host "System disk space check: OK" -ForegroundColor Green

}
Message formatting with the Write-Host Cmdlet

In this example, we first check the system memory using the Get-CimInstance cmdlet and then use the Write-Host cmdlet to display the results. If low memory is detected, the message is displayed in yellow text with a dark red background to indicate a warning. If memory is sufficient, the message is displayed in green text to indicate success.

We then perform a similar check for system disk space, again using the Write-Host cmdlet to format the output. If low disk space is detected, the message is displayed in yellow text with a dark red background. Otherwise, the message is displayed in green text to indicate success.

In PowerShell, you can redirect the output of the Write-Host cmdlet to a file or another stream using the redirection operator “>”. However, it is important to note that Write-Host writes output directly to the console, rather than to the standard output stream that can be redirected.

Managing Write-Host Stream output

If you try to redirect the output of Write-Host using the “>” operator, you will find that the output is not redirected as expected. Instead, you will get an empty file, because the output was sent to the console and not to the file.

To redirect the output of Write-Host, you can use the Start-Transcript cmdlet to start a transcript of the current session. This will capture all output that is sent to the console, including output from Write-Host. For example:

Start-Transcript -Path "C:\Output.txt"

Write-Host "This message will be redirected to a file"

Stop-Transcript
Managing Write-Host Stream output

In this example, the Start-Transcript cmdlet is used to begin capturing output to a file called “Output.txt”. The Write-Host cmdlet is then used to output a message to the console. When the script is finished, the Stop-Transcript cmdlet is used to stop capturing output.

After running this script, you can open the “Output.txt” file to see the message that was written using Write-Host. The transcript will include all console output from the session, so you can capture other output in addition to messages from Write-Host.

Overall, while Write-Host does not output to the standard output stream, you can still redirect its output by using the Start-Transcript cmdlet to capture all console output. This allows you to redirect the output of Write-Host and other console output to a file or other stream.

In conclusion, the Write-Host cmdlet is a powerful tool in PowerShell for displaying output to the console. By using the different formatting options available, such as color and background color, we can make our output more readable and visually appealing. For further information, you can visit the Microsoft PowerShell website for more information about this.