PS – DynamicParam

Hello together,

in this post i want to show you a very rare function attribute. It is called dynamicparam and as the name says it adds some features to create parameters dynamically when the user writes the execution function:

function Test-DynamicParam 
        $paramDictionary = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameterDictionary
        foreach ($Property in (1..$Count))
            $attributes = New-Object System.Management.Automation.ParameterAttribute
            $attributes.ParameterSetName = '__AllParameterSets'
            $attributes.Mandatory = $false
            $attributeCollection = New-Object -TypeName System.Collections.ObjectModel.Collection[System.Attribute]
            $Name = "DynamicParam_$Property"
            $dynParam = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameter($Name,
            [int32], $attributeCollection)
            $paramDictionary.Add($Name, $dynParam)
    {    "Entered: $Count"

Test-DynamicParam -Count 5 

In this example the user enters the function name and the parameter $count. As you can see in the dynamicparam block now additional parameters are created. If you enter for example the number 5 – 5 additional parameters show up and can be given to the function.


So you can add or remove parameters depending on the parameters which the user entered which can be very powerful if you write very complex functions.

Give it a look




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s