Friday Fun: Aren’t You Special

crayonsIf you’ve been following my work for any length of time you know I am constantly going on about “objects in the pipeline”. But PowerShell is flexible enough that you should be able to use it as it meets your needs, provided you know the limitations for whatever path you take. Sometimes you really just want to read information on the screen and you want it to be as meaningful as possible. For today’s fun let me show you how to add some special characters that might be just what you need to spice up an on screen display.

First, you need to know what you can display and how. You will be using the [CHAR] class. Don’t worry too much about details. You can use the [CHAR] class to display characters other than the usual alphanumeric and symbol characters on your keyboard. I also need to point out that everything I am showing you will only work in the PowerShell console, not the ISE. Here’s how you can see some of these characters.

This snippet is the same as displaying [char]1 through [char]6.

You’ll also find these ranges of interest.

Or use this one-liner to display them all.

Now that you know what is possible, let’s use some of these characters. How about a visual indicator if a service is running or not?

I’m creating a custom property called Indicator which will display a special character based on the service status. To make it pretty to read I’m formatting as a table and hiding the header since I can tell from the output everything I need to know.

Remember I mentioned limitations? The special characters cannot be sent to a file, converted to HTML or exported. All you can do is run the code and read the screen. But that may be all you require. Well, technically you can direct the output to a file with Out-File. You won’t see the characters in Notepad but you should when you use Get-Content.

Let’s take the service example to another level. Using the Win32_Service class from WMI, we can tell if a service is set to Auto start but is not running. Let’s use the same type of indicator for the service status and an indicator if an Auto start service isn’t running.

I used [CHAR]15 as my indicator. You know what? Since we know we can’t do anything with the output other than read it on the screen (you are allowed to do this), let’s go all out.

This code again uses WMI and the special characters, but then it looks at special character properties I created and determines a color to use for the output. The tricky part was converting the [CHAR] class back into an integer so that I could determine what color to use.

There are probably other ways to achieve the same result, but this works for me.

So have some fun with these special characters. Realize there are limitations to what you can do with the output, but there are probably no limitations on how you might use them. If you come up with something interesting I hope you’ll share.

Let’s have fun out there.