Throwing the Kitchen Sink at PowerShell

The other day I was watching a good intro video from Shane Young on getting started with PowerShell profiles. I use profile scripts extensively, and they can be extremely useful in configuring your PowerShell experience. One element you could add to your profile is a customized PowerShell prompt. Microsoft provides one by default. It creates a simple function called prompt. The best part is that you can define your own function called prompt, and PowerShell will run it every time you hit enter.

Continue reading

Prompting for the Holidays

This should wait for a Friday Fun post but since it is December 1st I decided not to wait. It is that time of year again and my PowerShell prompt is colorful and sparkly.


My holiday themed PowerShell prompt (Image Credit: Jeff Hicks)

In my profile I have this code to use a new Prompt function.

I’ve posted the custom prompt before but now that I’m using GitHub a bit more, I’ve posted it as a Gist which you can find at https://gist.github.com/jdhitsolutions/f85eb9474c04d40c79ec#file-pschristmasprompt-ps1.

You can’t really pass parameters to a Prompt function so if you want to customize it you need to edit the file. One things I’ve thought of is changing the text so that instead of “Christmas in…” it might say “Santa Comes”

Santa Prompt
Santa Prompt (Image Credit: Jeff Hicks)

Or maybe this:

Another holiday promptAnother holiday prompt (Image Credit: Jeff Hicks)

And it doesn’t take much effort to support other holidays.

A Hanukkah versionA Hanukkah version (Image Credit: Jeff Hicks)

You can find this version of the prompt function at https://gist.github.com/jdhitsolutions/c26c0a4fb2112ca6e74e#file-pshanukkahprompt-ps1.

Whatever your sentiment I hope you have a happy and healthy holiday season.

Update 2 December: It appears that some of the special characters I was using aren’t supported in Windows 10. [CHAR]14 is supposed to be a musical note and [CHAR]15 is like a snowflake. In Windows 10 they don’t display. You can select any other character you want, or none at all.

Friday Fun: Another Christmas Prompt

christmastree In last week’s Friday Fun post, I shared with you a PowerShell prompt that would display a festive Christmas countdown clock. This week I have another holiday related prompt function. This one is pretty straight forward and is not that much different from the default PowerShell prompt function.

I included some logic so that my customization only happens during the month of December and before Christmas. The prompt changes the background color of your console between DarkRed and DarkGreen. The first time you run it, the prompt will randomly select a color. You might want to run Clear-Host, or CLS after loading the prompt.

After that the background color will toggle leaving you with an effect like this:

christmasprompt2014-redgreen

It is interesting to see how different commands write to the console. You could even combine both of my prompts if you are in an especially festive mood.

christmasprompt2014-everything

There’s probably no practical value in this other than having some fun and maybe understanding some PowerShell scripting concepts. Hope your holiday shopping is going well. Have a great weekend.

Friday Fun Christmas Countdown Prompt

christmaslights It’s that time of year again where PowerShell can make all your wishes come true. Ok, maybe that’s a bit much, but PowerShell is the gift that keeps giving all year long. Again, maybe too much. How about this? Here’s a revised version of my Christmas countdown prompt. I’ve posted this in the past. But some of you are new to PowerShell and may have missed it. Plus I’ve revised it a bit.

In PowerShell you have a built-in function called Prompt. It controls what you see in the console which is usually something like PS C:\>. But you can create your own Prompt function. Here’s my Christmas countdown prompt.

I’ve inserted plenty of comments so you should be able to understand how it works. In a nutshell, the prompt inserts a message that indicates how much time remains before December 25th of the current year. The text also includes some special characters to help embellish it and put you in the holiday mood. Finally, each character is written to the host with a random color. You can see that I used a Switch construct to evaluate the random number with an expression.

This function will work best in the PowerShell console. If you want to try it out you can copy and paste this function into your console session. Your PowerShell session will now look like this:

christmasprompt2014

The prompt will only last for as long as your PowerShell session is running. The next time you start PowerShell you will be back to your original prompt. If you enjoy this, put my Christmas prompt function in your PowerShell profile, but be sure it is called Prompt. When the holidays are over simply comment out the function. Or you could add some logic to your profile.

Ho-Ho-Ho! Have a great weekend.

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.
time-prompt-1

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

time-prompt-2

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.