PowerShell Command Line Usage Quirks

I am a very big fan of Windows PowerShell, Microsoft’s new command shell and scripting language. I have found PowerShell useful in software testing and many other areas too. I am delivering a PowerShell talk and facilitating some hands-on PowerShell Lab sessions next week at the Microsoft Management Summit (see http://www.mms-2008.com/ ). MMS is Microsoft’s premier IT event and it’s very interesting. Anyway, as always, the hardest part about learning or teaching any new programming language or technology is getting over the initial hurdles and quirks. The screenshot below demonstrates some of the PowerShell command line usage quirks. I wrote a custom startup script so that each command would be numbered. My first command:
PS(1) C:\> write-host -object "hi" -backgroundcolor "red"
demonstrates a relatively verbose usage example. The write-host cmdlet can accept an -object parameter, which is what to print, and an optional -backgroundcolor parameter, which is, of course, the background color to use. My second command:
PS(2) C:\> write-host "hi"
shows that some (but not all) parameters can be passed by position and therefore the parameter name is optional, as in the case of the -object parameter. Optional parameters are general the first or second parameter(s) and are usually omitted simply to save typing. My third command:
PS(3) C:\> write-host -obj "hi"
shows that PowerShell is very intelligent about parsing your command line input, and can accept partial parameter nsames as long as the meaning is unambiguous. My fourth command:
PS(4) C:\> write-host ‘hi’
illustrates that PowerShell strings can use either double-quotes or single-quotes. The difference (not illustrated in this example) is that strings inside single-quotes are literals; but certain characters inside double-quotes will be evaluated (such as `n for a newline). My fifth command is:
PS(5) C:\> write-host hi
and shows you that PowerShell will do its best to infer a type. Here PowerShell correctly believes that "hi" is intended to be a string. In general I prefer to be explicit about putting quotes on strings but that’s just a matter of personal preference. My sixth command:
PS(6) C:\> "hi"
is quite strange and shows you that PowerShell really tries to infer what you mean. Here, the default behavior for a string is to display it. Quite unusual! My seventh command is:
PS(7) C:\> hi
The term ‘hi’ is not recognized as a cmdlet, function, operable program, or script file.
At line:1 char:2
+ hi <<<<
and shows that PowerShell can’t read your mind. My next two commands are:
PS(8) C:\> write-host "hi" -nonewline
hiPS(9) C:\> write-host "bye" -fore "yellow"
Some PowerrShell cmdlet parameters (in fact many) do not accept any values. These are called switch parameters in PowerShell terminology. Here I tell write-host to print "hi" but then do not print the default newline. My last command again illustrates that PowerShell only needs enough of a parameter name (-fore instead of the full -foregroundcolor) to unambiguously determine what to do.
These few examples should help get you over the initial quirks of using PowerShell on the command line if you’re new to PowerShell. Be sure to check out the MMS 2008 Web site. This year’s event is sold out but keep an eye out for next year’s event.

This entry was posted in Software Test Automation. Bookmark the permalink.