A Timely PowerShell Prompt

021913_2047_WordTest1.pngDuring the course of writing a few scripts that refresh a specific part of the console, such as the recent Read-Host alternative, I realized that flashing colors wasn’t always necessary. The fact that I could update the same space on the screen meant I could write the same content with minor changes and it would look like the the screen as “flipping”. Essentially I was thinking of a clock.

So I thought it might be handy to have a clock as part of my PowerShell prompt. PowerShell has a built-in function called Prompt but you can replace it with your own version. The function will only last for as long as your PowerShell session so if you don’t like it, exit and restart PowerShell.

This is for the most part the basic function that shows PS and your current location. This prompt function will not work properly in the PowerShell ISE. The magic happens by always setting the cursor to the same coordinates in the PowerShell shell console. I use the same type of While loop I used in my other functions, only this time I’m waiting for the user to press any key, which would indicate the start of typing a command. Once that has been detected, the looping stops and the time ceases to be refreshed in the prompt.

You really need to see this live but here’s a screenshot example.

Then I thought it might be helpful to have the clock stand out so I added a little color.


The only issue I’ve found with these prompts, is that if you need to scroll in the console window, you’ll need to press the spacebar or type something so that the clock stops refreshing. Otherwise you are scrolling while PowerShell is trying to write to the console.

Instead of clock you could use a countdown timer. Or perhaps some sort of performance counter. For a prompt though, you need to make sure you can get and display the information in a few hundred milliseconds, otherwise the prompt will feel sluggish and unresponsive.

Enjoy and let me know where this leads you.

Friday Fun Send a Colorful Message

Next week is Pluralsight’s 10th anniversary. In preparing for that happy event, I wanted to send a special greeting. Of course, because my courses are on PowerShell it only seemed appropriate to use PowerShell to display my message. In fact, let’s jump right to the result.


Here’s how I did it.

The script takes a string, in this case a here string stored as $msg, and writes each character to the console using Write-Host. I’m using Write-Host so that I can take advantage of foreground and background colors. I get a random color for each from a list of possible console colors, skipping the color used for the current console background. My script uses a Do loop to get a random color for the background that is different than what is chosen for the foreground. I only use a color scheme if there is a non-space character. I suppose I could have turned things around and tested with -match against \S.

Anyway, a short and simple script that gets the message across in a colorful way. Enjoy and have a great weekend.

Another PowerShell Valentine

I’ve been sharing some Valentine’s Day themed fun on Twitter today. This one is a bit too long to fit into a tweet.

To get the full effect you need to run in the console and not the ISE. It works in the ISE but you won’t get the “special” characters.

Updated Console Graphing in PowerShell

The other day Distinguished Engineer and PowerShell Godfather Jeffrey Snover posted a blog article about the evils of Write-Host. His take, which many agree with, is that Write-Host is a special case cmdlet. In his article he mentions console graphing as an example. I wrote such a script earlier this year. Mr. Snover’s post drove some new attention to my post and I realized it needed a little polishing.

Here is a revised version of that script.

I didn’t make too many structural changes other than to add Set-StrictMode and revise some of my IF statements to test for ParameterSetName instead of a variable. Using StrictMode, which is a good thing, caused problems in my earlier version. I also went through and added some new examples, including a few PowerShell 4.0.

The function still requires at least PowerShell 3.0. But it allows you to do something like this:


All you can do is look at this but sometimes, that’s all you need.

PowerShell Console Graphing Revised

Many of you have been having fun with my PowerShell Console Graphing tool I posted the other day. But I felt the need to make one more major tweak. I wanted to have the option for conditional formatting. That is, display graphed entries with high values in one color, medium in another and low in yet another.

The default behavior is still to use a single color. But using ParameterSets I added some new parameters, -HighColor, -MediumColor and -LowColor. They are all mandatory so if you use one parameter you have to define them all.

I also moved the Property parameter and made it positional which should make it easier to use. The conditional coloring works basically by taking the largest possible graph value, which is based on available screen width and dividing it into thirds. The top third is considered high, second third is medium and last third is low.

When it comes time to graph, I check which parameter set we’re using and set the graph color accordingly.

But now I can run a command like this:


What do you think? Download Out-ConsoleGraph-v2.

UPDATE: A newer version is available at http://jdhitsolutions.com/blog/2013/12/updated-console-graphing-in-powershell/