DEV Community

Cover image for PowerShell - Using Parameter Sets
Marcel.L
Marcel.L

Posted on • Edited on

4 2

PowerShell - Using Parameter Sets

💡 What are parameter sets in PowerShell and how to use them

Have you ever wondered when you are writing a PowerShell function or commandlet how you can make only certain parameters be presented to the consumer of the function in certain scenarios? That's where parameter sets come in. 😄

We will look at the following test function: [Test-ParameterSets] on exactly how this functionality can be used.

Function Test-ParameterSets {
[CmdletBinding(SupportsShouldProcess, DefaultParameterSetName="Default")]
Param (
[Parameter(Mandatory=$false)]
[string]$DefaultParameter1,
[Parameter(Mandatory=$false)]
[string]$DefaultParameter2,
[Parameter(Mandatory=$false, ParameterSetName="A")]
[Switch]$A,
[Parameter(Mandatory=$false, ParameterSetName="A")]
[string]$AParameter1,
[Parameter(Mandatory=$false, ParameterSetName="A")]
[string]$AParameter2,
[Parameter(Mandatory=$false, ParameterSetName="B")]
[Switch]$B,
[Parameter(Mandatory=$false, ParameterSetName="B")]
[string]$BParameter1,
[Parameter(Mandatory=$false, ParameterSetName="B")]
[string]$BParameter2
)
If ($A) {
Write-Output "Using Parameter Set A"
Write-Output $DefaultParameter1
Write-Output $DefaultParameter2
Write-Output $AParameter1
Write-Output $AParameter2
}
If ($B) {
Write-Output "Using Parameter Set B"
Write-Output $DefaultParameter1
Write-Output $DefaultParameter2
Write-Output $BParameter1
Write-Output $BParameter2
}
}
# Function Tests #
Test-ParameterSets -DefaultParameter1 'defaultValue1' -DefaultParameter2 'defaultValue2' -A -AParameter1 'valueA1' -AParameter2 'valueA2'
Test-ParameterSets -DefaultParameter1 'defaultValue1' -DefaultParameter2 'defaultValue2' -B -BParameter1 'valueB1' -BParameter2 'valueB2'

The first step is to add a DefaultParameterSetName="Default". We can set that in our [CmdletBinding()] as follow:



// code/demo-function.ps1#L2-L2

[CmdletBinding(SupportsShouldProcess, DefaultParameterSetName="Default")]


Enter fullscreen mode Exit fullscreen mode

By declaring a default parameter set name on our [CmdletBinding()] will set all of our parameters defined under the Default set. What we will do next is define which parameters needs to be presented if the parameter switch $A is used. We do not want to present parameters from switch $B in this case. We will do this by defining a new parameter set name and grouping the parameters we want to be part of that particular set.



// code/demo-function.ps1#L8-L13

[Parameter(Mandatory=$false, ParameterSetName="A")]
[Switch]$A,
[Parameter(Mandatory=$false, ParameterSetName="A")]
[string]$AParameter1,
[Parameter(Mandatory=$false, ParameterSetName="A")]
[string]$AParameter2,


Enter fullscreen mode Exit fullscreen mode

We will also give parameter switch $B and it's corresponding parameters, it's own parameter set name.



// code/demo-function.ps1#L14-L19

[Parameter(Mandatory=$false, ParameterSetName="B")]
[Switch]$B,
[Parameter(Mandatory=$false, ParameterSetName="B")]
[string]$BParameter1,
[Parameter(Mandatory=$false, ParameterSetName="B")]
[string]$BParameter2


Enter fullscreen mode Exit fullscreen mode

Now that we have defined our parameter sets and grouped the relevant parameters according to their sets our function/Cmdlet will now only present corresponding parameters based on which switch is used when calling the function/Cmdlet.

testFunctionAnimation

You can also find some very helpful documentation on parameter sets on Microsoft Docs.

I hope you have enjoyed this post and have learned something new. You can also find the code samples used in this blog post on my GitHub page. ❤️

Author

Like, share, follow me on: 🐙 GitHub | 🐧 X/Twitter | 👾 LinkedIn

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (2)

Collapse
 
cbergmeister profile image
Chris Bergmeister • Edited

Two suggestions:

  • Mandatory property on parameters default to false, therefore you could make the code easier to read by just removing all the Mandatory=$false bits. As an aside: When adding the Mandatory attribute one doesn't even need to assign a value, one can do just [Parameter(Mandatory], which makes it nicer to read. docs.microsoft.com/en-us/dotnet/ap...
  • Use .IsPresent property on switch parameters so that the person reading the code knows it's a switch paramter docs.microsoft.com/en-us/dotnet/ap...
Collapse
 
pwd9000 profile image
Marcel.L

Those are awesome tips @cbergmeister Thank you!

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay