This is a series of 3 blog posts on one of the best kept secrets in PowerShell: “Argument Completion”:
Now that we have run through how to create argument completers in Part 2, I wanted to cover when to create argument completers and more specific for which cmdlet parameters?
Which parameters should I focus on when building “Argument Completers”?
These are the criteria I have used when writing argument completers:
When you spend some time building argument completers, you really want to find the “hot spot” and provide completion for most used parameters.
Here’s some piece of code which provides an overview of the most occurring parameters within a specified module.
function Get-ArgumentCompleterCandidate
{
[Cmdletbinding()]
param(
[Parameter(Mandatory=$true)]
[Alias('ModuleName')]
[string]$Module,
[switch]$Descending
) $Cmdlets = Get-Command -Module $Module
Write-Verbose -Message "Module: $Module"
Write-Verbose -Message "Cmdlets found: $($Cmdlets.Count)" $Parameters = $Cmdlets.Parameters.Keys
Write-Verbose -Message "Parameters found: $($Parameters.Count)"
Write-Verbose -Message "Unique parameters found: $(($Parameters | Sort-Object -Unique).Count)" $CandidateParameters = $Cmdlets.Parameters.Values |
Where-Object {$_.ParameterType.Name -ne 'SwitchParameter'} |
Where-Object {$_.ParameterType.Name -ne 'ActionPreference'} |
Where-Object {$_.Name -notin ('PipelineVariable','OutVariable','OutBuffer','ErrorVariable','WarningVariable','InformationVariable')} |
Select-Object @{Name='ParameterName';Expression={$_.Name}},ParameterType
Write-Verbose -Message "Relevant parameters found: $($CandidateParameters.Count)"
Write-Verbose -Message "Relevant unique parameters found: $(($CandidateParameters | Sort-Object -Property ParameterName -Unique).Count)"
Write-Verbose -Message "Priority parameters for argument completion"
$CandidateParameters | Group-Object -Property ParameterName -NoElement | Sort-Object -Property Count -Descending:$Descending
}
From the CLI
PS C:\> Get-ArgumentCompleterCandidate -Module FailoverClusters
PS C:\> Get-ArgumentCompleterCandidate -Module GroupPolicy
PS C:\> Get-ArgumentCompleterCandidate -Module Hyper-V –Desc –Verbose
It’s a simple way of identifying the parameters you could/should focus on…
I will cover in some additional blog posts other advanced topics of “Argument Completers”, but that’s for the near future.
Hope this helps…
3 thoughts on “Building PowerShell auto-completion using TabExpansionPlusPlus – Part 3 – When?”