Search notes:

PowerShell parameter attribute: validateSet

The PowerShell function parameter attribute validateSet makes sure that the passed value for the parameter belongs to a given set of values.
Using validateSet is especially handy because the values in the set are used for tab completion.

Simple example

function do-something {

   param (
      [validateSet('foo', 'bar', 'baz')] [string] $doWhat
   )

   switch ($doWhat) {

      'foo' { write-host 'Foo!'  }
      'bar' { write-host 'Bar!'  }
      'baz' { write-host 'Baz!'  }

   }
}
Github repository about-PowerShell, path: /language/statement/function/parameters/attributes/validate/Set/intro.ps1
Invoking this function with a value that doesn't belong to the list results in an error message:
PS: > do-something  xyz
do-something : Cannot validate argument on parameter 'doWhat'. The argument "xyz" does not belong to the set "foo,bar,baz"
specified by the ValidateSet attribute. Supply an argument that is in the set and then try the command again. …

See also

Advanced functions

Index