PowerShell Write Cmdlets

 

PowerShell has multiple Cmdlets that display information, but the question that often arises is which one should be used? In this post we will take a look at the differences between some of the more common ways to display information in PowerShell and which one will fit best for a given situation. Before we get start it is important to define a few terms are going to pop up. This first term is Stream; which is a sequence of data elements made available over time. A stream can be thought of a items on a conveyor belt being processed one at a time rather than in large batches. The second is pipeline, whish consists of a chain of processing elements arranged so that the output of each element is the input of the next.

 

Write-Host vs Write-Output

Write-Host outputs text directly to the console / host, bypassing the pipeline, therefore it cannot be used to pipe output to another cmdlet, such as Out-File. However, this cmdlet can change the color of the text’s background and of the color of the text itself.

Write-Output places the object in the pipeline which can be consumed by other Cmdlets.  If  it is the last Cmdlet, it will display the output in the console/host. Unless it is necessary to display different colored messages or display a richer user interface it would be best to use Write-Output since it allows for easier information passing.

 

Write-Host_Piped_And_Not_Piped

 

Another major difference between these two cmdlets is that Write-Host will output the text that it is given on the same line unless otherwise specified, such as using the newline character “`n”.

Write-Output is going to insert a new line character between each string that it is given.

 

Capture

 

Write-Information

Windows PowerShell 5.0 introduces a new, structured information stream (number 6 in Windows PowerShell streams) that you can use to transmit structured data between a script and its callers (or hosting environment).

Write-Information lets you add an informational message to the stream, and specify how Windows PowerShell handles information stream data for a command. By default, Write-Information will not write anything to the screen unless $InformationAction is set to ‘Continue’,

this can be accomplished by either setting $InformationAction to ‘Continue’ when the cmdlet is written or by setting $InformationAction to ‘Continue’ at the top of the script. Write-Information is useful when program information only needs to be displayed under

special circumstances or only if the user requests it.

 

Write-Information

Write-Error vs Write-Warning

Write-Error and Write-Warning are fairly similar as they are both used to show information about a problem that occurred. Where they differ is what information gets written to the screen.

Starting in PowerShell V3, warnings have their own stream; which means that this stream can be captured and piped to another Cmdlet.

Write-Warning will also not write any output to the screen unless $WarningPreference has been set to ‘Continue’ at the top of the script or by setting $WarningPreference to ‘Continue’ when the cmdlet is written.

 

Write-Warning

 

The Write-Error cmdlet declares a non-terminating error. By default, errors are sent in the error stream to the host program to be displayed, along with output.

Since the messages from the Write-Error cmdlet are sent to the error stream, they can e accessed by indexing the $Error variable array, with the ‘0’ index being the most recent

error that was placed in the stream.

 

Write-Error

Write-Debug

Write-Debug is used when its necessary to output debug messages in a critical section of code that can be turned on and off or when a script  developer does not want the extra information to be displayed when the script is ran normally.

Write-Debug is similar to Write-Warning in that unless the variable $DebugPreference is set to ‘Continue’ at the beginning of a file or before the cmdlet will be executed nothing will be displayed to the screen.

The debug parameter can also be used if it necessary for the user to acknowledge that something has occurred.

Debug Message

Write-Progress

The Write-Progress cmdlet displays a progress bar in a Windows PowerShell command window that depicts the status of a running command or script.

You can select the indicators that the bar reflects and the text that appears above and below the progress bar.

The Write-Progress command includes a status bar heading (“activity”), a status line, and the variable $I (the counter in the For loop), which indicates the relative completeness of the task.

 

writeprogressworking (2)

 

The Cmdlets discussed in this post are not the only was the PowerShell can display information, PowerShell has many more Cmdlets that have the ability to display information

or redirect that information. However,  the cmdlets discussed here will work for most day to day uses, with these cmdlets it is possible to write to the pipeline, display warnings, debug information,

and Errors.