Linked ValidateSets

I’d like to provide validation to a script that would contain “linked” parameters validated using the ValidateSet attribute. Here’s a contrived example of what I’m talking about (obviously this syntax is wrong):

[ValidateScript('Dog', 'Cat', 'Fish')]
[string] $Animal,

[ValidateScript(‘SiberianHusky’, ‘GoldenLab’)] # Dog
[ValidateScript(‘Siamese’, ‘MaineCoon’)] # Cat
[ValidateScript(‘GoldFish’, ‘Beta’)] # Fish
[string] $Breed

For example, if I select 'Dog' then I would only want to validate $Breed with 'SiberianHusky' and 'GoldenLab'. Is there an easy way of doing this or do I need to do some sort of Dynamic Parameter?

I think, What you are referring to is [ValidateSet()] attribute. You cannot have multiple declaration of ValidateSet for a Parameter Set(here only one and is default). You can club this with [ValidateScript()] attribute like below.

function t {
    [ValidateSet('Dog', 'Cat', 'Fish')]
    [string] $Animal,

        $Temp = $_
        switch ($Animal){
            'Dog' {[ValidateSet('SiberianHusky', 'GoldenLab')]$Breed = $Temp;$true}
            'Cat' {[ValidateSet('Siamese', 'MaineCoon')]$Breed = $Temp;$true}
            'Fish' {[ValidateSet('GoldFish', 'Beta')]$Breed = $Temp;$true}
    [string] $Breed

I think this is a clean solution. Thanks @kvprasoon!

ValidateScript() does however lose the ability to tab-complete possible values. One alternative is to also implement an ArgumentCompleter in order to examine any already-specified parameters.

Additionally, I don’t think you can actually reference other parameters from ValidateScript; it operates outside the function scope. You would need dynamicparam here, potentially. I suspect that ValidateScript will not be particularly effective; if you add a default switch case that throws (as anyone would if you want proper validation) it would likely throw every time.

Based on your mention of ArgumentCompleter, I found this post that looks like it accomplishes all needs:


From what I’ve seen, DynamicParams doesn’t get the value of the defined Parameters, its possible to check whether the parameter binding, but not the value it bound to and ValidateScript understand the parameter and its value, But it depends on the order of Binding. In above example fails for below usage

... -Breed Goldfish -Animal Fish