Friday Fun – A PowerShell Console Menu

When working in PowerShell, and especially when scripting, you might want to give the user a choice of actions. For example, you might develop a configuration script that another admin or technician will run. Perhaps one of the steps is to configure networking depending on their location so you want to give the person running the script a menu of choices. Here’s one way you might accomplish this, without resorting to graphical tools or WinForms. Continue reading

Friday Fun Drive Usage Console Graph

I think you’ll like this. Normally, I prefer my PowerShell commands to write objects to the pipeline. But there’s nothing wrong with sending output directly to the console, as long as you know that the output is intended only for the screen. What I find frustrating is the use of Write-Host when really, pipelined objects would be better. But for today, I’m going to revel in the beauty of the console and create a colorized drive utilization graph. Continue reading

PowerShell Deep Dive Treasure

Without a doubt the PowerShell Deep Dive conference was one of the best meetings I’ve ever attended and I wanted to share one tidbit I came away with that I find immensely useful and never knew. During one of Bruce Payette’s talks he tossed out, practically as an aside, a reference to searching command line history with the # character. What!!???

I never heard of this and it is very slick. From the command prompt (either the console or ISE) enter the # character followed by whatever part of a previous command you remember. It does not have to be the start of the command, it can be any part of it. Then press TAB. PowerShell will search your history buffer and return the first match. If there are multiple matches you can keep pressing TAB to cycle through them. Awesome! The beauty for me is that it just brings the command back to the prompt so you can edit it.

Thank you Bruce!

Content Redirection

Here’s another item I see in some submisstions of the 2010 Scripting Games that I felt I should address: the use of legacy console redirection. While technically not illegal or wrong, an example like this demonstrates (at least in my opinion) that the scripter hasn’t fully adopted the PowerShell paradigm.

While this will work and create a text file, it is not fully written in the PowerShell spirit. At the very least, you should use the cmdlets Out-File, Add-Content or Set-Content in place of the legacy console redirection operators, > and >>.   I’ll admit that 99 times out of 100 you’ll have no issues with the resulting text file.  However, there is the potential for encoding issues. The cmdlets are better designed to handle encoding as well as a few other PowerShell bells and whistles like –noclobber.

Another reason I suggest using cmdlets is that the > and >> operators are actually part of the underlying command session that PowerShell.exe is running on. It’s conceivable that some future version of PowerShell or some PowerShell hosted application won’t support these operators which means your script will fail and need revision. Stick with cmdlets from the beginning to protect your scripting investment.

Here’s a better PowerShell implementation that uses cmdlets and takes better advantage of the pipeline.

In this example I’m using Tee-Object which not only displays the properties in the console but also redirects to the text file. I’m sure you’ll agree this is better than using > and >>.  Or am I making too much out of this?

Cool Custom Consoles

Ok, maybe this isn’t as slick as something from West Coast Customs but maybe you’d like to add a little style to your PowerShell session. I’m talking about the (by now) staid blue console. Perhaps you don’t like blue or the color contrast isn’t too your liking. Here are some things to try if you want to customize your console.

Continue reading