Tag Archives: Write-Host

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.