Colorful Information in PowerShell 5.0

Posted on January 28, 2016 by Jeff Hicks in PowerShell with 0 Comments

color-aspect-hero

In a previous article I introduced you to the new information stream in PowerShell 5.0. I think this is something that you can incorporate into your own scripts and functions. As I mentioned last time, one of the drawbacks to using PowerShell’s Write-Information cmdlet is that if you want to see messages, then it’s difficult to distinguish between output and messages. Fortunately, Microsoft’s PowerShell team planned ahead and tweaked Write-Host in PowerShell 5.0.

In the past the PowerShell community has frowned upon the use of Write-Host, especially as the means to display results. But it has always been fine for informational messages, especially if you take advantage of the ForegroundColor or BackgroundColor parameters. If you take advantage of those parameters, then you can easily tell the difference between actual output and messages. In PowerShell 5.0, Write-Host is a wrapper for Write-Information. This means you can use Write-Host and get all the benefits of the new information stream.

To demonstrate, here’s a variation on the test function that I used previously.

Let’s run the test function.

Using Write-Host as an information wrapper (Image Credit: Jeff Hicks)

Using Write-Host as an information wrapper (Image Credit: Jeff Hicks)

You’ll notice that I specified an information variable. The information preference doesn’t have an effect on Write-Host. But I have information.

Information data (Image Credit: Jeff Hicks)

Information data (Image Credit: Jeff Hicks)

Everything worked as planned, but I probably need to make additional decisions as to what information to display. By the way, you can also use Write-Information in the same command as Write-Host. Messages for the former will only be displayed if you use an InformationPreference of continue while messages for the latter will always be displayed.

Or so I thought until I came up with the approach.

Sometimes I might want to occasionally see messages from Write-Host, so I’ll check the value of $InformationPreference. If it’s set to continue, then I know to display the messages with Write-Host. To simplify my code, I’ll create a Boolean variable, $Info, at the beginning. Throughout the function, if this value is True, then the Write-Host lines will execute.

Testing with no information (Image Credit: Jeff Hicks)

Testing with no information (Image Credit: Jeff Hicks)

Sponsored

As expected, no information is generated.

Writing information in color (Image Credit: Jeff Hicks)

Writing information in color (Image Credit: Jeff Hicks)

Writing information in color (Image Credit: Jeff Hicks)

Now my Write-Host messages are displayed in color, and I have data in the information variable.

Write-Host information (Image Credit: Jeff Hicks)

Write-Host information (Image Credit: Jeff Hicks)

The PSHost tag is automatically added, and you can see that the source is Write-Host. You can manually add additional tags if you desire.

Adding additional tags (Image Credit: Jeff Hicks)

Adding additional tags (Image Credit: Jeff Hicks)

But I’m probably not going to do that and neither are you. Instead, here’s a proof of concept that lets you add additional tags to each piece of information.

I can look at the bound parameters and discover the name of the information variable being used. Then, every time I want to insert a new tag, I can update the variable value. In this last iteration of the function, I’m adding the same tags I used in the very first version of the function. Now I have the best of everything. I can display information messages in color and get property tagged data.

Testing additional tags (Image Credit: Jeff Hicks)

Testing additional tags (Image Credit: Jeff Hicks)

As you can see, the information object has my additional tags.

Displaying the additional tags (Image Credit: Jeff Hicks)

Displaying the additional tags (Image Credit: Jeff Hicks)

I would probably create a small helper function to streamline the process of adding additional tags. Please note that this is only necessary when using Write-Host. If you use Write-Information, you can specify all the tags you need.

Sponsored

I’ve probably given you a lot to think about to take some time to experiment and test. When you get ready to take advantage of this feature, I hope you’ll let me know how you are using this new stream and your experiences.

Sponsored

Tagged with ,