Example showing how to override TabExpansion2 in Windows PowerShell 3.0 Example showing how to override TabExpansion2 in Windows PowerShell 3.0 powershell powershell

Example showing how to override TabExpansion2 in Windows PowerShell 3.0


I think this example should give you a good starting point: Windows Powershell Cookbook: Sample implementation of TabExpansion2. The example code shows that you can add code both before and after the default calls to [CommandCompletion]::CompleteInput.

For instance, you can add an entry to the $options hashtable named CustomArgumentCompleters to get custom completion for command arguments. The entry should be a hashtable where the keys are argument names (e.g. "ComputerName" or "Get-ChildItem:Filter") and the values are arrays of values that could be used to complete that parameter. Powertheshell.com also has an article about this: Dynamic Argument Completion. You can also specify custom completions for native executables, using the NativeArgumentCompleters option (again, keys are command names and values are arrays of possible completions).

OnceCompleteInput has returned, you can store the result in $result for further analysis. The result is an instance of the CommandCompletion class. If the default completion didn't find any matches, you can add your own CompletionResult entries to the list of matches:

$result.CompletionMatches.Add(   (New-Object Management.Automation.CompletionResult "my completion string") )

Don't forget to return $result from the function so the completion actually happens.

Finally, a note on troubleshooting: the code that calls TabCompletion2 seems to squelch all console-based output (not surprisingly), so if you want to write debugging messages for yourself, you might try writing them to a separate text file. For instance, you could change the End function in TabCopmletion2 to look like this:

$result = [System.Management.Automation.CommandCompletion]::CompleteInput(    $inputScript, $cursorColumn, $options)$result | Get-Member | Add-Content "c:\TabCompletionLog.txt"$result


Here is an example of overridden TabExpansion2 -TabExpansion2.ps1and several used in practice profiles with completers for it:

The points of interest:

  • TabExpansion2.ps1 does minimum work on loading. Potentially expensiveinitialization is performed once when completion really happens.
  • Overridden TabExpansion2 provides extension mechanism via one or moreprofiles *ArgumentCompleters.ps1 in the path. Profiles are invoked onceon the first call of TabExpansion2. Several profiles may come withdifferent independent modules, tools, and etc. and used simultaneously.
  • In addition to standard custom argument completers andnative argument completers this custom TabExpansion2 supportsresult processors which tweak the results from the built-in completionand input processors which can intercept and replace the built-in completion.
  • It works around read only empty built-in results in some cases.
  • ArgumentCompleters.ps1contains an example of an input processor witch replaces the built-incompletion of types and namespaces with an alternative, more usefulsometimes.
  • Another completer provides completion in comments: help tags (.Synopsis,.Description, etc.) and completion of commented out code, why not?