Powershell parameter sets and optional parameters


I’m writing a function for which two parameters should be exclusive and optional.

Here are valid inputs:

Here is an invalid input:

Here is my code so far:

Here is what I get when I call Get-Help:

However here is the error I get when I try to call the function with only the two mandatory parameters:

I’m missing something here, but I can’t figure out what…

How can I get two parameters that are mutually exclusive and optional?


This makes perfect sense. You have 3 parameter sets, and the 2 mandatory parameters are included on every set. How could PowerShell determine which set you meant to use?

Luckily the [CmdletBinding()] attribute can take a parameter that helps with this exact case: DefaultParameterSetName. Setting this allows PowerShell to use this set in the case of (certain) ambiguities. Use it like so:

Note that in this case, you named it default; it could have been named anything.


Powershell parameter sets and optional parameters by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply