July 4, 2025

USEFUL-IT

A blog for USEFUL-IT information

PS : DynamicParam

How to use on DynamicParam in a function based upon input from a parameter

Cmdlet Dynamic Parameters

Cmdlets can define parameters that are available to the user under special conditions, such as when the argument of another parameter is a specific value. These parameters are added at runtime and are referred to as dynamic parameters because they are added only when they are needed. For example, you can design a cmdlet that adds several parameters only when a specific switch parameter is specified.

Get more info about these DynamicParam : https://msdn.microsoft.com/en-us/library/dd878299(v=vs.85).aspx

function get-info {

Param(
    [Parameter()]
    [ValidateSet('A', 'B')]
    [string] $Target
)

DynamicParam {
        # Ensure $Target is defined
        try { [void]$Target }
        catch { $Target = [string]::Empty }

        if ($Target -eq 'B') {
            $target += "...si si"
        }
    }
end {
        Write-Host $Target
    }
}

About The Author