Understanding TabExpansion2 in PowerShell
Welcome back to Wahmans Powershell Blog! Today we are diving into the TabExpansion2 cmdlet—a powerful helper function that can enhance your tab-completion experience inside PowerShell. Whether you’re creating powerful modules or simplifying daily scripting tasks, understanding how TabExpansion2 works can really up your PowerShell game.
According to Microsoft, TabExpansion2 is “a helper function that wraps the CompleteInput() method of the CommandCompletion class to provide tab completion for PowerShell scripts.” In simpler terms, this is how custom tab-completion is implemented in your PowerShell functions and scripts.
Why Should You Care? This functionality is incredibly useful when you are creating scripts or functions and want to improve the user experience—as if you were using built-in PowerShell commands. You can guide users by providing automatic suggestions (e.g., parameter values) while they’re typing your commands.
Let’s Look at 4 Examples from Beginner to Advanced!
1. Beginner: View Default TabExpansion2 Output
If you’ve never played with TabExpansion2, a good starting point is to inspect its default behavior.
# Basic call to see how TabExpansion2 works
TabExpansion2 -inputScript 'Get-Pr' -cursorColumn 7
This will show you completions available when you type “Get-Pr” and hit tab at the 7th character. It’s helpful in debugging or understanding what PowerShell suggests by default.
2. Intermediate: Adding Simple Static Tab Completion
Here’s how to make a custom function with TabExpansion2 support to complete static values:
function Get-EnvironmentInfo {
param(
[Parameter(Position=0)]
[string]$Environment
)
switch ($Environment) {
"Dev" { "Development" }
"Test" { "Testing" }
"Prod" { "Production" }
default { "Unknown" }
}
}
Register-ArgumentCompleter -CommandName Get-EnvironmentInfo -ParameterName Environment -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
[System.Management.Automation.CompletionResult[]]@(
'Dev','Test','Prod' | ForEach-Object {
New-Object System.Management.Automation.CompletionResult $_, $_, 'ParameterValue', $_
}
)
}
Now when you type Get-EnvironmentInfo
and press tab at the -Environment
parameter, PowerShell will suggest “Dev”, “Test”, or “Prod”.
3. Advanced: Dynamic Completion Based on Filesystem
Register-ArgumentCompleter -CommandName Get-LogFile -ParameterName FileName -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
$path = "C:\\Logs"
Get-ChildItem -Path $path -Filter "*.log" | ForEach-Object {
New-Object System.Management.Automation.CompletionResult $_.Name, $_.Name, 'ParameterValue', $_.FullName
}
}
function Get-LogFile {
param(
[Parameter(Mandatory)]
[string]$FileName
)
Get-Content -Path "C:\\Logs\\$FileName"
}
This tab-completion will suggest available .log files from the C:\Logs directory when executing Get-LogFile
.
4. Expert: Full InputScript Expansion with TabExpansion2
Here’s an example of how you can directly invoke TabExpansion2 and analyze the completions:
$inputScript = 'Get-Process -Name no'
$cursor = $inputScript.Length
$results = TabExpansion2 -InputScript $inputScript -CursorColumn $cursor
$results.CompletionMatches | ForEach-Object { $_.CompletionText }
This provides you with all potential completions PowerShell has for this partial command. You can use this to build advanced debugging tools or even a custom PowerShell console interface.
Conclusion
TabExpansion2 might not be the most used cmdlet directly, but it is foundational to customizing PowerShell’s powerful tab-completion behavior. Once you get comfortable with it, your scripts and modules can feel as polished as the built-in cmdlets.
Happy scripting, and I will see you in the next post!
Leave a Reply