Powershell Advanced Functions: How to tell which parameter set a user has selected

I wrote an advanced Powershell function yesterday that would allow a user to change the value of a single Active Directory attribute on all of our staff accounts or on all of our student accounts in bulk.  The function enabled the user to use a -AccountType parameter which then gave only the options of our Staff or Student accounts.  This meant that whoever used the function would not need to worry about OU locations \ searchbases etc – just select Staff or Students from the intellisense…easy peasy.

While this was a great success, it quickly became apparent that they also wanted the same functionality on single user accounts.

Using the same function, I simply added an additional parameter of ‘Identity’ (which would take an individual SAMAccount name) and made it part of an additional parameter set.  This way, if they wish to make a bulk change they use the -AccountType parameter which will then not display the -Identity parameter as it belongs to a different param set.  Likewise, if they wish to make an attribute change to a single user, they use the -Identity parameter and they will not have the -AccountType parameter available to them.  It’s not my intention to go into multiple parameter sets – for that, take a look at:

help about_Functions_Advanced_Parameters

So how can you tell which parameter set the user has selected?  Turns out to be quite easy:
Simply query the:

$PSCmdlet.ParameterSetName

You can see an example on line 63 in the function I wrote.  It’s called: ‘Populate-OHADAttribute‘ and you can find it in my Powershell repo on Github .

Be the first to comment

Leave a Reply

Your email address will not be published.


*