Share via


Windows PowerShell Tab Expansion

So you think it’s pretty nice that Cmd.exe uses tab expansion for file paths? Just wait until you see what Windows PowerShell does with 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 disadvantages of 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’re working in a command window and 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 D then you’re done: the full folder name will be displayed (including the double quote marks) and you can simply press ENTER and the command will run. 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 then 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 while working in the PowerShell console? 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’t seen 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 we are) 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:

g

et-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 -detailed that 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-ChildItem cmdlet:

• Type get-h and press TAB.

• Press the spacebar, then type get-ch and press TAB.

• Press the spacebar, then type and press TAB. Keep pressing TAB until you see –Detailed and 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-Command cmdlet includes the following parameters:

• -CommandType

• -Module

• -Syntax

• -TotalCount

• -Noun

• -Verb

• -Name

• -ArgumentList

• -Debug

• -ErrorAction

• -ErrorVariable

• -WarningAction

• -WarningVariable

• -OutBuffer

• -OutVariable

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

Get-Command –CommandType cmdlet

On the other hand, because the -CommandType parameter is the only Get-Command parameter whose name begins with the letter C you can add this parameter merely by typing -c:

Get-ChildItem –c cmdlet

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’s a 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

Now you can applaud.