Dynamic parameters are a special breed of parameters that can be shown or hidden based on runtime conditions. Your PowerShell functions could for example have just one parameter called -Action, and based on the action the user chooses, additional parameters would show. Or, you could have parameters show only when the user has Administrator privileges.

Unfortunately, composing dynamic parameters isn’t trivial. With the help of a module called “dynpar”, using dynamic parameters becomes just as easy as using “normal” static parameters, and you then can simply designate dynamic parameters with a new attribute called [Dynamic()] that tells PowerShell the condition that needs to be met to show the parameter:

    # regular static parameter
    # show -Lunch only at 11 a.m. or later
    [Dynamic({(Get-Date).Hour -ge 11})]
    # show -Mount only when -Path refers to a local path (and not a UNC path)
    [Dynamic({$PSBoundParameters['Path'] -match '^[a-z]:'})]

You can find a thorough step-by-step walk-through here: https://github.com/TobiasPSP/Modules.dynpar

Twitter This Tip! ReTweet this Tip!