Does anyone have an example showing how to override the TabExpansion2 function in Windows PowerShell 3.0? I know how to override the old TabExpansion function, but I want to provide a list of items for the intellisense in PowerShell ISE. I looked at the definition of TabExpansion2 and it wasn’t easily understandable how I inject my own code in the the tab expansion process.
Does anyone have an example showing how to override the TabExpansion2 function in Windows
Share
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
$optionshashtable 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).Once
CompleteInputhas returned, you can store the result in$resultfor further analysis. The result is an instance of theCommandCompletionclass. If the default completion didn’t find any matches, you can add your ownCompletionResultentries to the list of matches:Don’t forget to return
$resultfrom the function so the completion actually happens.Finally, a note on troubleshooting: the code that calls
TabCompletion2seems 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 theEndfunction inTabCompletion2to look like this: