Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell Parameter list of values

Tags:

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?

like image 332
Serge111 Avatar asked Feb 08 '14 01:02

Serge111


People also ask

What is param () in PowerShell?

The PowerShell parameter is a fundamental component of any script. A parameter is a way that developers enable script users to provide input at runtime. If a PowerShell script's behavior needs to change in some way, a parameter provides an opportunity to do so without changing the underlying code.

How do I set parameters in PowerShell?

To create a parameter set, you must specify the ParameterSetName keyword of the Parameter attribute for every parameter in the parameter set. For parameters that belong to multiple parameter sets, add a Parameter attribute for each parameter set.

How do I pass multiple parameters in PowerShell?

To pass multiple parameters you must use the command line syntax that includes the names of the parameters. For example, here is a sample PowerShell script that runs the Get-Service function with two parameters. The parameters are the name of the service(s) and the name of the Computer.


1 Answers

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 
like image 144
Keith Hill Avatar answered Nov 09 '22 06:11

Keith Hill