I'm looking for a way to make a cmdlet which receives parameter and while typing, it prompts suggestions for completion from a predefined array of options.
I was trying something like this:
$vf = @('Veg', 'Fruit')
function Test-ArgumentCompleter {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[ValidateSet($vf)]
$Arg
)
}
The expected result should be:
When writing 'Test-ArgumentCompleter F', after clicking the tub button, the F autocompleted to Fruit.
To add to the other helpful answers, I use something similiar for a script I made for work:
Documentation for Register-ArgumentCompleter is well explained on Microsoft Docs. I personally don't like to use the
enum
statement as it didnt allow me to uses spaces in my Intellisense; same for theValidate
parameter along with nice features to add a description.Output:
EDIT:
@Mklement made a good point in validating the argument supplied to the parameter. This alone doesnt allow you to do so without using a little more powershell logic to do the validating for you (unfortunately, it would be done in the body of the function).