Can PowerShell Parameters Belong To Multiple Parameter Sets?

Say you’ve got a function that takes three parameters: Username, ComputerName and SessionName, but you don’t want someone to use ComputerName and SessionName at once. You decide to put them in separate parameter sets. Awesome, except you want Username to be a part of both parameter sets and it doesn’t look like you can specify more than one.

This will generate an error:

So how do you make a parameter a member of more than one parameter set? You need more [Parameter()] qualifiers.

They chain together and you now $Username is a part of both parameter sets.

3 thoughts on “Can PowerShell Parameters Belong To Multiple Parameter Sets?

    1. Yep. This is just a simple example to illustrate the concept. You’d use this if you wanted a parameter to belong to some parameter sets but not others.

Comments are closed.