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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With