The new TabExpansion feature…

PowerShell Team

One of the nicest new features in the latest drop of Windows PowerShell is enhanced tab-completion. We now tab-complete properties on variables and parameters on cmdlets in addition to the old filename completion. But that’s not the interesting part. The cool bit is that it’s done through a user-definable function. In the same way that you can redefine your prompt, you can also define custom tab completion.  Here’s how it works:


When you hit tab after typing some text, the function TabExpansion is called to generate the list of possible completion matches. You can find this function by doing:


PS (16) > ls function:*tab*


CommandType     Name                                      Definition

———–     —-                                      ———-

Function        TabExpansion                             


And see the current definition:


PS (17) > $function:tabexpansion


  # This is the default function to use for tab expansion.




Or save it to a text file so you can edit it:


PS (18) > $function:tabexpansion > c:\temp\tabexpansion.ps1

PS (19) > notepad $$


(The $$ above refers to the last token in the previous command – in this case, it’s the name of the file we saved the function too.)


The parameter declaration for this function is


           param($line, $lastWord)


where $line is the full command line as typed, and $lastWord is the last word or token in the line. So – if the line is:

            cd c:\windows

then the last word would be “c:\windows”. If the line is

            cd “c:\program files”

the last word would be “c:\program files”.


These two pieces are provided because most expansions work on the last token (e.g. property name expansion) but some require the entire line (e.g. parameter expansion.)


The TabExpansion function should return an array of strings representing the possible matches. The host will store these strings in a circular buffer the first time you hit tab after typing some text. Subsequent tabs will step you through the list of possible matches. Typing anything other than a tab will reset the sequence.


If this function returns null, then the expansion process will fall through to the old built-in file-name completion code to try and generate a match. (The file name completion stuff is still implemented in the host as compiled code. It was too much work to migrate it to the script for v1.)


Since TabExpansion is a function, it can do anything – pop up menus, create GUIs, talk to the user, or whatever. The one thing to keep in mind is performance – you want to get the results back quickly. If you have a complex completion scenario that is too slow in script then you might need to write a helper cmdlet.


I’m really looking forward to seeing what the community can do with this feature!


Bruce Payette

Windows PowerShell Technical Lead


PSMDTAG:INTERNAL: Tab Expansion, Tab Completion

PSMDTAG:SHELL: Tab Expansion, Tab Completion
PSMDTAG:FAQ: Why doesn’t tab completion do xxxx?



Discussion is closed.

Feedback usabilla icon