Skip to content
AgileLogics
4 min read

Argument Completion

Prerequisites: Pipeline-aware functions

Great tools offer tab completion for their parameters. ValidateSet and enums complete fixed values; for dynamic values, add a completer.

ArgumentCompleter

function Get-Project {
  param(
    [ArgumentCompleter({
      param($commandName, $parameterName, $wordToComplete)
      'website', 'webapi', 'worker', 'mobile' |
        Where-Object { $_ -like "$wordToComplete*" }
    })]
    [string]$Name
  )
  "Opening project $Name"
}

$completion = TabExpansion2 -inputScript 'Get-Project -Name we' -cursorColumn 20
$completion.CompletionMatches.CompletionText -join ', '
website, webapi

The script block receives the partial word and returns suggestions — which can come from anywhere: folder names, a config file, or an API. (TabExpansion2 is what pressing Tab calls; it's used here to show the result.)

ArgumentCompletions (PowerShell 7)

Suggestions without restricting input:

# illustrative
function Set-Region {
  param([ArgumentCompletions('eastus', 'westeurope', 'uksouth')][string]$Region)
  "Region: $Region"
}

Unlike ValidateSet, any value is still accepted.

Register-ArgumentCompleter

Add completion to commands you didn't write — including native programs:

# illustrative
Register-ArgumentCompleter -Native -CommandName deploy -ScriptBlock {
  param($wordToComplete)
  'dev', 'test', 'prod' | Where-Object { $_ -like "$wordToComplete*" }
}

Many CLI tools (dotnet, gh, kubectl, winget) print a PowerShell completion script you can add to your profile.