Search code examples
powershellparameters

PowerShell Parameter list of values


I've been trying to search for an answer, but I think that having the words PowerShell and parameter together in a set of keywords are not making for an easy search

My question is, I'm writing a function, and I've supplied a parameter, but the parameter has to be one of a list of specific strings.

Is there a way I can supply these strings within the script so that if I type "myfunction -parameter .." I can use tab completion for the parameter value?


Solution

  • If you are on PowerShell V2 you can use the [ValidateSet()] attribute e.g.:

    param(
        [Parameter()]
        [ValidateSet('foo','bar','baz')]
        [string[]]
        $Item
    )
    

    See the help topic by executing:

    man about_functions_advanced_parameters