User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active
 

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 started it is important to define a few terms that 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 as items on a conveyor belt being processed one at a time rather than in large batches.
  • The second is Pipeline, which 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 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.

Function Test-Output {
    Write-Output "SAPIEN Technologies"
}

Function Test-Output2 {
    Write-Host "SAPIEN Technologies" -ForegroundColor Green
}

Function Receive-Output {
    process { Write-Host $_ -ForegroundColor Yellow }
}

#Output piped to another function, not displayed if first
Test-Output | Receive Output

#Output not piped to another function, only displayed if first
Test-Output2 | Receive Output

#Pipeline sends to Out-Default at the end
Test-Output

SAPIEN Technologies
SAPIEN Technologies
SAPIEN Technologies

 

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.

#Write-Host will output all text on a single line
Write-Host "SAPIEN"
Write-Host "Technologies"

#Write-Host will output text on multiple lines Write-Host "SAPIEN`n"
Write-Host "Technologies"
#Write-Output will also output text on a multiple lines
Write-Output "SAPIEN"
Write-Output "Technologies"

SAPIEN Technologies
SAPIEN
Technologies
SAPIEN
Technologies

 

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. Starting in Windows PowerShell 5.0, Write-Host is a wrapper for Write-Information.

#This won't be written to the screen
Write-Information -MessageData "First Message"

#This will be written to the screen
Write-Information -MessageData "Second Message" - InformationAction Continue

#This will also be written to the screen
$InformationPreference = 'Continue'
Write-Information -MessageData "Third Message"

Second Message
Third Message

 

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 will not output to the Console
Write-Warning "First Error Statement"

#Write-Warning will output to the Console
Write-Warning "Second Error Statement" - WarningAction Continue

#Write-Warning will output to the Console
$WarningPreference = 'Continue'
Write-Warning "Third Error Statement"

WARNING: Second Error Statement
WARNING: Third Error Statement

 

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 be accessed by indexing the $Error variable array, with the ‘0’ index being the most recent error that was placed in the stream.

PS C:\>
Write-Error -Message "Error Statement" Write-Error -Message "Error Statement" : Error Statement
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyqualifiedErrorID : Microsoft.PowerShell.Commands.WriteErrorException

PS C:\> #The First index of the $Error Variable will be the most recent error
$Error[0] Write-Error -Message "Error Statement" : Error Statement
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyqualifiedErrorID : Microsoft.PowerShell.Commands.WriteErrorException

 

Write-Debug

Write-Debug is used when it's 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 run 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 is necessary for the user to acknowledge that something has occurred.

PS C:\> Write-Debug "SAPIEN Technologies"

PS C:\> Write-Debug "SAPIEN Technologies" - Debug
DEBUG: SAPIEN Technologies

PS C:\> $DebugPreference = 'Continue'
PS C:\> Write-Debug "SAPIEN Technologies"
DEBUG: SAPIEN Technologies

PS C:\>

 

 

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.

Updating
   Progress->
   [oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo

   OuterLoop
Updating
   Progress->
   [oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo

   InnerLoop
PS C:\>
PS C:\>
PS C:\>
PS C:\>
PS C:\>
PS C:\> for( $I = 1; $I -lt 101; $I++ )
>> { Write-Progress -Activity Updating -Status 'Progress->;' -PercentComplete $I -CurrentOperation OuterLoop;
>> for( $j = 1; $j -lt 101; $j++ )
>> { Write-Progress -Id 1 -Activity Updating -Status 'Progress->' -PercentComplete $j -CurrentOperation InnerLoop } }

 

The cmdlets discussed in this post are not the only ways that 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.

If you have questions about our products, please post in our support forum.
For licensed customers, use the forum associated with your product in our Product Support Forums for Registered Customers.
For users of trial versions, please post in our Former and Future Customers - Questions forum.
Copyright © 2024 SAPIEN Technologies, Inc.