Can I use a constant in the ValidateSet attribute of a PowerShell function parameter?

No, it has to be a literal or a scriptblock. The scriptblock option seems pointless since it seems to use the literal (string) value of the scriptblock instead of executing it.

So effectively, from my testing, you must use literals.

If you use a dynamic parameter instead you could achieve this, but that's way overkill just to be DRY.

If you try to use a variable, it won't work (and ISE will give you the red squiggly). The help text erroneously says it must be a constant, but it means literal.

I created a constant with:

Set-Variable -Option Constant

And it still does not work.

Tags:

Powershell