Parameter Sets

Introduction

When working with PowerShell, you’ll often need to use parameter sets.

In this article, we’ll take a look at how to create and use parameter sets in PowerShell. We’ll also see some examples of how parameter sets can be used to simplify complex scripts.

By the end of this post, you should have a good understanding of how PowerShell’s parameter sets work and how they can be used to your advantage.

What is Parameter Sets

Parameter sets allow you to specify different groups of parameters for a command. When we specify a parameter using autocomplete, we won’t be able to use parameter in another set.

Many commands in PowerShell have more than one parameter sets. This can be checked using Get-Help command. For example, executing Get-Help Get-Item will show two parameter sets of Get-Item command.

Parameter Sets Example

How to use Parameter Sets

Parameter Sets are declared using ParameterSetName property of Parameter attribute. In order to know which parameter sets we are currently in, we can use $PSCmdlet.ParameterSetName. This way we can differentiate logic based on the set. Below script is the example of using Parameter Sets.


param (    
    [Parameter(Mandatory = $true, ParameterSetName = 'Set1')]
    [int]
    $Even,

    [Parameter(Mandatory = $true, ParameterSetName = 'Set2')]
    [int]
    $Odd
)

if ($PSCmdlet.ParameterSetName -eq 'Set1') {
    $counter = 1
    while ($counter -le $Even) {
        if ($counter % 2 -eq 0) {
            Write-Host $counter
        }
    
        $counter++
    }
}
else {
    $counter = 1
    while ($counter -le $Odd) {
        if ($counter % 2 -eq 1) {
            Write-Host $counter
        }
    
        $counter++
    }
}

In another sense, Parameter Sets allow us to overload the function, the similar concept derived from Object Oriented Programming (OOP) where there can be many methods with the same name and return value but different parameters. The difference is PowerShell function allows us whether we want to return value or not at the same function. Below is the example:


param (    
    [Parameter(Mandatory = $true, ParameterSetName = 'Set1')]
    [int]
    $Even,

    [Parameter(Mandatory = $true, ParameterSetName = 'Set2')]
    [int]
    $Odd
)

if ($PSCmdlet.ParameterSetName -eq 'Set1') {
    $counter = 1
    while ($counter -le $Even) {
        if ($counter % 2 -eq 0) {
            Write-Host $counter
        }
    
        $counter++
    }
}
else {
    $counter = 1
    $sum = 0
    while ($counter -le $Odd) {
        if ($counter % 2 -eq 1) {
            $sum += $counter
        }
    
        $counter++
    }

    return $sum
}

Suppose we execute the script using the second parameter set, we can pipe the output to another script. In this case, there will be another script named Test-ValueFromPipeline.ps1 as below.


param (    
    [Parameter(ValueFromPipeline)]
    [int]
    $param1
)

$param1 = $param1 * 3

Write-Host $param1

If we pipe or propagate the result of function overloading script to Test-ValueFromPipeline.ps1 using .\Function-Overloading.ps1 -Odd 10 | .\Test-ValueFromPipeline.ps1, the output will be 75.

Conclusion

Using Parameter Sets can be a great way to organize your PowerShell commands and make them easier to use. With Parameter Sets, you can have different sets of parameters for different tasks. This means that you can call the right set of parameters for the task at hand without having to remember which parameters go together. In addition, using Parameter Sets can help make your commands more readable and less error-prone.