Mutual Exclusive Parameters

by May 12, 2015

Sometimes, PowerShell functions have parameters that should be mutually exclusive: the user should only be able to use either one, not both.

To create mutually exclusive parameters, assign them to different parameter sets, and make sure you define a default parameter set name (which is used if PowerShell cannot automatically pick the right parameter set):

function Test-ParameterSet
    [Parameter(ParameterSetName='number', Position=0)]

    [Parameter(ParameterSetName='text', Position=0)]


The function Test-ParameterSet has two parameters: -id and -name. The user can only specify one, not both at the same time. The sample also illustrates how you find out which one the user picked.

Twitter This Tip! ReTweet this Tip!