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.

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 -Path C:\Folder -ItemType Directory
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.

Write-Verbose -Message 'Attempting to create folder C:\Folder...'
$null = New-Item -Path C:\Folder -ItemType Directory -ErrorAction SilentlyContinue
if (-not (Test-Path -Path 'C:\Folder')) {
    Write-Error -Message 'Could not create C:\Folder'
} else {
    Add-Content -Path C:\Folder\textfile.txt -Value 'testing'
    if (-not (Get-Content -Path C:\Folder\textfile.txt)) {
        Write-Warning -Message 'Created folder but could not create file.'

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.

Subscribe to 4sysops newsletter!

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.


Leave a reply

Your email address will not be published.


© 4sysops 2006 - 2022


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


Log in with your credentials


Forgot your details?

Create Account