Tab Expansion

 

 

One of the big advantages of using Windows PowerShell is that you can just sit at the command prompt and type everything; there’s no need to mess around with dialog boxes or mouse clicks or any other GUI nonsense. On the other hand, one of the big disadvantagesof Windows PowerShell is that you have to sit at the command prompt and type everything: you can’t take advantage of dialog boxes or mouse clicks or any other GUI shortcuts.

 

In other words, if you’re the kind of person who likes to type commands from the command prompt then Windows PowerShell is like a dream come true. But what if you don’t like to type, or what if you find it difficult to type? In that case, you’re just plain out of luck, aren’t you?

 

Well, no, not entirely. Granted, a certain amount of typing will always be required in Windows PowerShell. However, Windows PowerShell also includes a few features that can dramatically reduce the amount of typing required. And one of the coolest of these features is tab expansion.

 

Tab expansion isn’t anything new; in fact, many of you are probably familiar with the tab expansion capabilities built into Cmd.exe. For example, suppose you want to change to the C:\Documents and Settings folder. To do that you need to type this entire command, right?

 

cd "c:\documents and settings"

 

Well, you can if you want to. Alternatively, you can simply type the following and then press the TAB key:

 

cd c:\d

 

If the Documents and Settings folder is the only folder in the root of drive C whose name begins with the letter Dthen you’re done: press ENTER and the command-shell will automatically fill in the remainder of the path for you (and – best of all – insert the double quote marks as well). But what if you have three or four folders whose names begin with the letter D? That’s fine: just keep pressing the TAB key and the command shell will dutifully cycle through the complete set of folders whose names begin with the letter D. When you finally hit the desired folder press ENTER and let the command shell do your typing for you.

 

When it comes to file and folder paths Windows PowerShell has this exact same capability. Want to switch to the C:\Documents and Settings folder? All you have to do is type the following and then press the TAB key:

 

cd c:\d

 

If necessary, keep pressing TAB until you see C:\Documents and Settings. At that point press ENTER and – like magic – you’ll instantly be transported to the Documents and Settings folder.

 

Wait, hold your applause; as the saying goes, you ain’tseen nothin’ yet. Say you want to use the Cmdlet Get- AuthenticodeSignature. If you’re young and in tip-top physical condition you can type that entire Cmdlet name yourself. If you’re rich, you can outsource the job and have someone do all that typing for you. Or, if you’re lazy (like the Scripting Guys) you can simply type the following and then press the TAB key:

 

get-au

 

That’s right: in Windows PowerShell tab expansion not only works with file and folder paths, but it works with Cmdlet names as well. Speaking of Cmdlets, is that one Cmdlet Get- PSSnap-in? Or is it Get- PSSnapin? Or are we way off; maybe it’s Get- PowerShellSnapin? To tell you the truth, we don’t remember. But that’s OK; all we have to do is type the following and then start pressing the TAB key:

 

get-p

 

In no time at all we’ll find exactly what we’re looking for: Get- PSSnapin.

 

Tab expansion even works with Cmdlet parameters. For example, the Get-Help Cmdlet includes a parameter named -detailedthat returns detailed help about a topic (including examples). You say you like the detailed help but you hate having to type –detailed? Then don’t. Try this trick to get detailed help about the Get- ChildItemCmdlet:

 

·          Type get-hand press TAB.

·          Press the spacebar, then type get- chand press TAB.

·          Press the spacebar, then type and press TAB. Keep pressing TAB until you see –Detailedand then press ENTER.

 

Pretty slick, huh?

 

As long as we’re on the subject, here’s another typing shortcut for you. When it comes to Cmdlet parameters you only have to type as much of the parameter name as is needed for Windows PowerShell to know exactly which parameter you’re referring to. For example, the Get- ChildItemCmdlet includes the following parameters:

 

·          - codeSigningCert

·          -Debug

·          - ErrorAction

·          - ErrorVariable

·          -exclude

·          -filter

·          -force

·          -include

·          - literalPath

·          -name

·          - OutVariable

·          -path

·          - recurse

·          -Verbose

 

Need to use the – codeSigningCertparameter? Well, if you want to you can type in the entire parameter name, like so:

 

get- childitem cert:\. - recurse codesigningcert

 

On the other hand, because the – codeSigningCertparameter is the only Get- ChildItemparameter whose name begins with the letter Cyou can add this parameter merely by typing -c:

 

get- childitem cert:\. - recurse –c

 

Nice. By the way, did we mention that tab expansion also works for variable names? Suppose you were silly enough to name a variable $ TheMainVariableThatGetsUsedInMyScript. That’sa lot of typing, especially for a variable name. So then don’t type it; just type the following and press TAB any time you need to refer to that variable:

 

$ th

 

Cool. Here’s one more. Suppose you use the New-Object Cmdlet to create an instance of Microsoft Excel:

 

$a = new-object - comobject excel.application

 

Now, type the following, then start pressing the TAB key and see what happens:

 

$a.

 

Nowyou can applaud.