3
[Parameter (Mandatory=$False)]
[ValidateSet("Val1", "Val2", "Val3", "Val4", "Val5",ignorecase=$true)]
[string[]] $configs = ""

Is there a way to alter the above so that I can accept several of the enum values in one go?

I'm hoping to be able to launch the script as so:

.\MyAwesome-Script.ps1 -config Val1 Val2 (or any combination of enum values as parameters)

But I need this to also be tab complete-able (is that even a word?)

For completeness, I'm using PS 4.0 and PSCX 3.1 is also installed

1 Answer 1

8

Just make your variable an array and it'll work just fine. Given the following function:

function Test-ValidateSet
{
    PARAM(
        [ValidateSet("Val1", "Val2", "Val3")]
        [string[]]$MyParam
    )

    foreach($value in $MyParam)
    {
        Write-Host "Parameter given: $value"
    }
}

For the above method, I get tab-completion on the MyParam parameter. To enter an array for the parameter, just separate the values with comma characters.

Test-ValidateSet -MyParam Val1, Val2, Val3

This supports tab-completion in both the PowerShell console and the PowerShell ISE.

Sign up to request clarification or add additional context in comments.

4 Comments

Sorry, made a small edit. Your response is almost identical to mine, but only the first param is tab completed. Or is that the limit of it?
You didn't include the actual parameter definition, so I did not know. If you are using the PowerShell ISE and expect the drop-down list you have to press CTRL+SPACE to get the drop-down list for additional entries, but pressing Tab to cycle through options should work just fine in both the PowerShell console and the PowerShell ISE (at least both do on my computer).
You are indeed correct. My apologies. Hmmm, the same behaviour is not exhibited on mine. CTRL+SPACE does indeed bring down a drop down, but after the first enum value is chosen, no addition enum values may be used for that same parameter, even though I am trying to populate an array.
And you have added the comma character between the two values?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.