Prof. Powershell

You Complete Me: PowerShell's Tab Completion Feature

PowerShell's tab completion feature saves lots of typing. In version 3, this features gets even better.

Despite the fact that there's a lot of typing involved when using PowerShell as an interactive shell, it has always had a few features to minimize the amount of actual keystrokes. One feature is tab completion. In version 2.0 you could begin typing a cmdlet and hit the Tab key:

PS C:\> Get-<tab>

After the dash if you press Tab, PowerShell will cycle through all of the commands that start with Get-. If you press Shift+Tab you can reverse direction. Let's say you end up with Get-Process and want to use the name parameter:

PS C:\> Get-Process -<tab>

If you press Tab after the dash, PowerShell will now cycle through all parameters. I'm using a dash but you can enter a character or two of the parameter and press Tab.

In PowerShell 3.0, it gets even better. Now we have completion for actual values and enumerations. In v3 you might get to this point:

PS C:\> Get-Process -Name

After -Name, press the space bar and then start pressing Tab. PowerShell will begin cycling through all the possible process names! Unfortunately, one area where this won't work is with Get-WMIObject. You can't tab your way through all available classes. However, in v3.0 tab complete also works for enumerations.

For example, the -Foregroundcolor parameter of Write-Host needs a value like Green or Cyan. Try this in a v3.0 session:

PS C:\> write-host "This is cool" -ForegroundColor <tab>

After the parameter and a space, pressing Tab will cycle through all the foreground color options. Even though it looks like I typed a lot, this command really only took a handful of keystrokes by using tab completion:

PS C:\> Write-Host "This is cool" -ForegroundColor Black -BackgroundColor Gray

This also works in the ISE so there's no reason you can't use full cmdlet and parameter names in your scripts.

If you don't have PowerShell 3.0 handy might be hard to visualize so take a look at this short YouTube clip that shows this feature in action.

If you are still on v2.0 and haven't been using tab completion, you are making life in the shell much more complicated than it needs to be. PowerShell is an automation engine in many ways, so make sure you are harnessing all of its power.

Note: Some of the information in this article is based on the latest publicly available pre-release version of PowerShell 3.0. Expect some changes with the final, shipping release.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular