PurpleScreen blog about open source

Dynamic parameters

Today we’re going to touch on dynamic parameters available in advanced functions. These are parameters that are added at run-time depending on the environment. The great example is the parameters that are dynamically exposed to the current parameter set of Get-Item cmdlet based on PS provider (FileSystem, Registry) used.

Faced it once, you should have found out that simply adding such a parameter to the function means writing a bunch of code. Let’s make a life easier!

Parameters created with this function are not available in script’s body using the parameter’s name but are exposed via $PSBoundParameters variable.

All the parameter attributes can be set by passing its values in hashtable to -Options. Other common attributes (mandatory switch, position, accepting values from pipeline) are also available. To add multiple dynamic parameters you should pipe one function call to another. Braindead simple! Here is an example:

New-DynamicParam -ParameterName Solution `
                 -ParameterType string `
                 -Mandatory `
                 -Options @{ValidateNotNullOrEmpty = $null} |
New-DynamicParam -ParameterName Effort `
                 -ParameterType int `
                 -Mandatory `
                 -Options @{ValidateSet = (1,2,3)}