PowerShell Makes You an Expert Cab Driver!

I travel a good bit for work, and I noticed a strange trend lately. Has anybody other than me noticed that more cab drivers don’t seem to know where they’re going? Now, don’t get me wrong; I mean that in the nicest possible way. Cab drivers are regular folk, just out there trying to earn a living, after all. So why am I picking on them? It’s because I remember a day, not too long in the past, when a cab driver could be counted on to perform a valuable service – namely, take you to any destination you could name. And (call me crazy) it seems to me that this is changing, for better or worse.

I flew to Atlanta a few months back and made my way to the taxi stand. Settling into my seat, I half-heard something from the front seat that I presumed was equivalent to, “Where to, Mac?” I named my hotel and the street. The driver started to pull away from the curb, but in seconds pulled back over to the curb and stopped. He visibly thought about something and then, wordlessly, he grabbed and handed to me… his GPS receiver. It was up to me to key in the location I wanted to go.

Maybe I’m all alone in this, but I find this terribly strange. I would think that one of the first things they teach you in cabbie school is where all the hotels are and how to get to them — right after the part about collecting money from the customer, and maybe right before the bit about being polite and friendly.  I always presumed that one of the things that a cab driver brings to the table in any transaction is knowledge of the local area, you know?

Maybe I’m just showing my age.

What does all this have to do with PowerShell? Glad you asked. Microsoft just released the second CTP (that’s Community Technology Previews) of the forthcoming PowerShell 3.0. The version that comes on your Windows 7 clients and your Windows Server 2008 R2 servers is the 2.0 release, so Microsoft is giving the PowerShell community an opportunity to kick the tires on the new version before it goes prime-time. There are some great new features in there, but some of them make me worry that I may soon need to turn in a job application at AAA A1 Taxi Company.

Automatic Module Imports

Historically, PowerShell required its users to manually direct the shell to add new modules of functionality. Import-Module is part of the PowerShell core cmdlets for precisely that purpose. This allows PowerShell (for performance reasons) to start up with the minimally necessary set of features to be useful while allowing the user to add the modules that contain the functionality needed for specialized tasks.

PowerShell 3.0 is going to remove that requirement. You will now be able to invoke any cmdlets, and, if the corresponding module isn’t imported, PowerShell will import it on your behalf! To activate this feature requires the use of the PSModuleAutoLoadingPreference variable — so it is possible to turn it off or require the intended user of a module to name the module and the cmdlet in order to invoke the cmdlet.

Tab-completed Enumerations

Another nice feature is an extension to PowerShell’s tab-completion capabilities. PowerShell already supports the ability to press the Tab key to ask PowerShell to fill in the remainder of a lengthy cmdlet name or the name of a parameter. PowerShell users have long been able, for example, to type Get-ADFin<tab> and see the cmdlet name completed to Get-ADFineGrainedPasswordPolicy or to type Write-Host –Fore<tab> and see the parameter expand to –ForegroundColor. PowerShell 3.0 will go beyond that to incorporate tab completion on Enumerations.

Enumerations are sets of values available to PowerShell that are managed in a collection. Specifically, enumerations are a listing of constant values, each referenced by a name. You’ve used enumerations, perhaps without knowing it, when using certain parameters that require you to supply one of a constrained list of values. Consider calling Set-ExecutionPolicy, which can take parameters like Restricted, Unrestricted, RemoteSigned, and others. Or consider the ForegroundColor parameter to Write-Host that I just mentioned, which takes parameters of Red, Green, Cyan (but not Blue). Both of these parameters can accept only a particular set of predefined values. And, thanks to PowerShell 3.0, I can fill them in with tab completion! Under PowerShell 3.0, I can type Set-ExecutionPolicy Un<tab> and see it fill in Unrestricted. I can type Write-H<tab>  -F<tab> M<tab> and see it expand that text out to Write-Host –ForegroundColor Magenta. That’s a savings of about twenty keystrokes!

Not that I’ll be using magenta for any of my text, of course — maybe a nice, responsible DarkCyan. But I can save some keystrokes when doing either and still create nice readable code!

Getting Your PowerShell Hack License

Be careful with these new tools. Remember that with great PowerShell comes great responsibility! Remember that just because you have the ability to name a cmdlet and see PowerShell launch it for you doesn’t mean that you shouldn’t be busily studying your lists of modules and which cmdlets are provided by each. Just as I fear for the day when I need a cab and solar flares have knocked out the GPS system, I worry that my dear readers may find themselves needing a key cmdlet on a computer that has not been upgraded to PowerShell 3.0 and find themselves as lost as that one Atlanta cab driver. Stay familiar with your local terrain. Know how to find your way around even when these clever shortcuts fail you. Don’t let these handy features blunt your knowledge of how PowerShell actually works, or perhaps it’ll be your voice I hear calling “Where to, Mac” from the front seat of my cab!

In this article

Join the Conversation

1 comment

  1. Mike Hammond Reply

    Comments, as always, are welcome!