Powershell Writing Messages to the Console Window
Powershell redirection operators
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_redirection?view=powershell-5.1
The redirection operators enable you to send streams of data to a file or the Success output stream.
The PowerShell redirection operators use the following numbers to represent the available output streams:
The PowerShell redirection operators are as follows, where n represents the stream number. The Success stream ( 1 ) is the default if no stream is specified.
Write-Host
https://wiki.labs.quest.com/display/DM/Write-Host
This is quite simply the cmdlet to use to write information to the console that you want the user of your script see. The output written this way is always displayed. It has color support too! You could be creative and use colors in a way that makes sense for your application while conveying the intent. Write-Ouput may behave like Write-Host but that is for a different purpose as we will see. In fact its ONLY if you need the richer output for a user interface you should use Write-Host.
Write-Output
https://wiki.labs.quest.com/display/DM/Write-Output
One of the great benefits of PowerShell is its pipeline mechanics that enable the output of one command to be passed to the next command in the pipeline. That output is not sent as basic text but rather the actual objects generated from a command are sent in the native object format to the next command.The output returned by Write-Output is passed to pipeline so the functions/cmdlets after the pipeline can read this for further processing. This is not possible if you use Write-Host.
If you possibly want to write data to the screen, but also want the data to be passed down the pipeline to further commands, then use Write-Output. You may have also heard of echo, but this is just an alias for Write-Output (run 'Get-Alias echo' to confirm this). It should be noted that for the most part you should use Write-Output as the goal for PowerShell is automation and Write-Host interferes with that automation and stops the output being able to be captured. Typically Write-Output is a better option and if you want to just convey information to users you can use Write-Verbose
Write-Debug
The Write-Debug cmdlet writes debug messages to the console from a script or command.
By default, debug messages are not displayed in the console, but you can display them by using the Debug parameter or the $DebugPreference variable.
Write-Error
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-error?view=powershell-5.1
Write-Error is to be used for non-terminating errors – meaning, the execution will continue past the Write-Error line after writing the error to the error stream. Write-Error has a lot of parameters that lets you customize it. The key among them is the Category parameter. Although it used to signal non-terminating errors, you can in fact stop the execution by setting the environment $ErrorActionPreference.
Write-Information
The Write-Information cmdlet specifies how Windows PowerShell handles information stream data for a command.
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. Information streams also work for PowerShell.Streams, jobs, scheduled jobs, and workflows.
The $InformationPreference preference variable value determines whether the message you provide to Write-Information is displayed at the expected point in a script's operation. Because the default value of this variable is SilentlyContinue, by default, informational messages are not shown. If you don't want to change the value of $InformationPreference, you can override its value by adding the InformationAction common parameter to your command. For more information,
see about_Preference_Variables and about_CommonParameters.
Write-Warning
The Write-Warning cmdlet writes a warning message to the Windows PowerShell host. The response to the warning depends on the value of the user's $WarningPreference variable and the use of the WarningAction common parameter.
Write-Verbose
The Write-Verbose cmdlet writes text to the verbose message stream in Windows PowerShell. Typically, the verbose message stream is used to deliver information about command processing that is used for debugging a command.
By default, the verbose message stream is not displayed, but you can display it by changing the value of the $VerbosePreferencevariable or using the Verbose common parameter in any command.
Reference Link:
-
https://4sysops.com/archives/powershell-streams-write-host-write-output-write-verbose-write-error/
-
https://sqljana.wordpress.com/2017/05/16/thoughts-on-when-to-use-write-host-write-output-write-debug-write-warning-write-verbose-write-errorthrow/
-
https://techibee.com/powershell/powershell-write-host-vs-write-output/2207
-
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-debug?view=powershell-5.1
-
https://www.itprotoday.com/powershell/write-output-or-write-host-powershell
-
https://channel9.msdn.com/shows/Going+Deep/Expert-to-Expert-Erik-Meijer-and-Jeffrey-Snover-Inside-PowerShell/