Quick tip: Conditional logic based on Parameter Set Name

When writing advanced functions with two (or more) parameter sets, sometimes you need to use conditional logic based on what parameter set is in use. This can be achieved easily by using the built-in PSCmdlet variable.

if ($PSCmdlet.ParameterSetName -eq 'ParameterSet1') {
    Write-Host 'You are using the "ParameterSet1" parameter set'
}

else {
    Write-Host 'You are using the "ParameterSet2" parameter set'
}

This code snippet shows you how to use the ParameterSetName property of the PSCmdlet variable (which is actually an object of type System.Management.Automation.PSCmdlet) to check what parameter set is in use. In this example I have two parameter sets, named ‘ParameterSet1’ and ‘ParameterSet2’.

Leave a Reply

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

WordPress.com Logo

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

Facebook photo

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

Connecting to %s