The PowerShell cmdlets Write-Host, Write-Output, Write-Verbose, Write-Error, and Write-Information use different streams to produce output in the console. Learn when and how to work with PowerShell streams.
Profile gravatar of Adam Bertram

Adam Bertram

Adam is a Microsoft Cloud and Datacenter Management Most Valuable Profressional (MVP) who specializes in Windows PowerShell. You can reach Adam at adamtheautomator.com or on Twitter at @adbertram.
Profile gravatar of Adam Bertram

Whenever you see some text come up in a PowerShell console, that text may not necessarily be the same "type." Whether the text is white, red, green, or whatever, that text may come from one of five different streams. Streams are essentially like roads the output you see in the console takes to get there. The code that's responsible for generating that output decides on which route to send it. There are many different reasons to send output down each "road," and it's up to the scripter to decide which path is best to take.

As of PowerShell v5, there are essentially six different kinds of streams: output, verbose, warning, error, debug, and information. Each of these streams is for various purposes and behaves a little bit differently. To aid in understanding this, you can think of a few of these streams as forming a hierarchy. These streams are debug, verbose, warning, and error in that order. Similar to how syslog represents severity in messages, PowerShell also adopts this concept of streams by severity.

Applying this stream concept to a real piece of code, let's say you've got a script that creates a folder that looks something like this:

New Item

New Item

By default, this will send some output to the pipeline, which we won't cover a lot here. Most well-written commands automatically send output to the pipeline. You can think of this output as the output stream; most commands work in this standard way.

However, let's say we want to spice this code up a bit. We need to add some error handling and logging to it. We now need to introduce other streams.

Above you can see that we've used the Write verb to return three different kinds of streams depending on the severity we'd like to convey.

We used Write-Verbose to send "nice to have" information to the console to let us know where it is in the process. Creating the folder is super important here, so I'm ensuring this occurs. If not, I'm using Write-Error to send a non-terminating error (error stream) to the console showing the read text. Finally, I'm also creating a file inside of the folder, but it's not important enough to be an error. Thus, I'm using Write-Warning to send some text down the warning stream to warn the user this happened.

Below you can see what each kind of output looks like. We did not include the debug stream in this example because it isn't typical to use the debug stream. Other than returning output via the debug stream, it also serves a purpose in debugging, which is an entirely different topic.

Stream output

Stream output

Finally, we've got the information stream. This is a new stream introduced in PowerShell v5 as a replacement or addition to the Write-Host cmdlet. This stream doesn't just send the text to the console like Write-Host does, but it also sends information to the pipeline as well.

Using the information stream is a great way to provide both high-level reporting of code status but also capture that in a variable to do something with later. Think of the information stream that Write-Information creates as verbose output but at a "higher" level.

In our example above, it might be useful to call Write-Information only if everything is successful or failed at the end. You could use the information stream to give an overall status update rather than providing the up-to-the-minute details like we are doing with Write-Verbose.

There's a lot of other material around streams we didn't cover in this article. If you need to dive deeper into streams and look for ways to show or hide stream output, redirect streams, and so on, I highly encourage you to check out June Blender's post on the "Hey, Scripting Guy!" blog.

Win the monthly 4sysops member prize for IT pros

Share
1+

Related Posts

0 Comments

Leave a reply

Your email address will not be published. Required fields are marked *

*

CONTACT US

Please ask IT administration questions in the forum. Any other messages are welcome.

Sending
© 4sysops 2006 - 2017

Log in with your credentials

or    

Forgot your details?

Create Account