# Parameter Set question

**URL:** <https://forums.powershell.org/t/parameter-set-question/14403>\
**Category:** PowerShell Help\
**Created:** [May 20, 2020, 11:04am UTC](https://forums.powershell.org/t/parameter-set-question/14403 "2020-05-20T11:04:30Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![hmartin](https://avatars.discourse-cdn.com/v4/letter/h/d07c76/32.png) [@hmartin](https://forums.powershell.org/u/hmartin)\
**Post date:** [May 20, 2020, 11:04am UTC](https://forums.powershell.org/t/parameter-set-question/14403/1 "2020-05-20T11:04:30Z")

</div>

I have the following function definition:

`function Get-MyService {`  
`[CmdletBinding()]`  
`[OutputType([MyService])]`  
`param`  
`(`  
`[Parameter(ParameterSetName = ‘main’)]`  
`[switch]$AutoButNotRunning,`  
`[Parameter(ParameterSetName = ‘main’)]`  
`[string]$ComputerName,`  
`[Parameter(ParameterSetName = ‘main’,`  
`ValueFromPipeline = $true,`  
`ValueFromPipelineByPropertyName = $true)]`  
`[string]$Name = “",`  
 _`[Parameter(ParameterSetName = ‘main’)]`  
 `[string]$DisplayName = "`_`”,`  
`[Parameter(ParameterSetName = ‘main’)]`  
`[switch]$IncludeExcludedServices,`  
`[Parameter(ParameterSetName = ‘showFiltered’,`  
`Mandatory = $true)]`  
`[switch]$ShowFilteredServices`  
`)`

This successfully isolates the ShowFilteredServices parameter into its own parameter set. It is also legal to run the cmdlet with no parameters, which also works.

However, I have been unsuccessful (through the use of parameter sets) in one other objective. The IncludeExcludedServices switch parameter should require the existence of the AutoButNotRunning parameter. The AutoButNotRunning parameter does not require the IncludeExcludedServices parameter and the AutoButNotRunning parameter can be used in conjunction with any combination of parameters that are part of the ‘main’ parameter set.

I don’t want to use dynamic parameters unless that’s the only way to accomplish this. Currently, I’m using the following code to deal with it, but that’s a cheap workaround. Any suggestions are greatly appreciated.

`if ($PSBoundParameters.ContainsKey(‘IncludeExcludedServices’) -and (-not $PSBoundParameters.ContainsKey(‘AutoButNotRunning’))) { <throw error> }`

&nbsp;

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [May 20, 2020, 11:52am UTC](https://forums.powershell.org/t/parameter-set-question/14403/2 "2020-05-20T11:52:08Z")

</div>

It’s required because you are using $PSBoundParameters as a splat? Rather than throw an error, you could just update $PSBounParameters:

```
function Get-MyService {
    [CmdletBinding()]
    param (
        [Parameter(ParameterSetName = 'main')]
        [bool]$AutoButNotRunning = $true,
        [Parameter(ParameterSetName = 'main')]
        [string]$ComputerName,
        [Parameter(ParameterSetName = 'main',
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true)]
        [string]$Name = "*",
        [Parameter(ParameterSetName = 'main')]
        [string]$DisplayName = "*",
        [Parameter(ParameterSetName = 'main')]
        [switch]$IncludeExcludedServices,
        [Parameter(ParameterSetName = 'showFiltered',
        Mandatory = $true)]
        [switch]$ShowFilteredServices
    )
    begin{
        if ($PSBoundParameters.ContainsKey('IncludeExcludedServices') -and (-not $PSBoundParameters.ContainsKey('AutoButNotRunning'))) { 
            Write-Verbose 'Adding param AutoButNotRunning'
            $PSBoundParameters.Add('AutoButNotRunning',$true)
        }
    }
    process{      
        $PSBoundParameters
    }
    end{}
}

Get-MyService -IncludeExcludedServices -Verbose
```

Output:

```
VERBOSE: Adding param AutoButNotRunning

Key Value
--- -----
IncludeExcludedServices True 
Verbose True 
AutoButNotRunning True
```

It depends on how exactly you are referencing the Param in the code within the function. If you are referencing $AutoButNotRunning, then using bool can have a default var.

```
function Get-MyService {
    [CmdletBinding()]
    param (
        [Parameter(ParameterSetName = 'main')]
        [bool]$AutoButNotRunning = $true,
        [Parameter(ParameterSetName = 'main')]
        [string]$ComputerName,
        [Parameter(ParameterSetName = 'main',
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true)]
        [string]$Name = "*",
        [Parameter(ParameterSetName = 'main')]
        [string]$DisplayName = "*",
        [Parameter(ParameterSetName = 'main')]
        [switch]$IncludeExcludedServices,
        [Parameter(ParameterSetName = 'showFiltered',
        Mandatory = $true)]
        [switch]$ShowFilteredServices
    )
    begin{
        Write-Verbose ('AutoButNotRunning: {0}' -f $AutoButNotRunning)
        if ($PSBoundParameters.ContainsKey('IncludeExcludedServices') -and (-not $PSBoundParameters.ContainsKey('AutoButNotRunning'))) { 
            Write-Verbose 'Adding param AutoButNotRunning'
            $PSBoundParameters.Add('AutoButNotRunning',$true)
        }
    }
    process{      
        $PSBoundParameters
        
    }
    end{}
}

Get-MyService -IncludeExcludedServices -Verbose
```

Output:

```
VERBOSE: AutoButNotRunning: True

Key Value
--- -----
IncludeExcludedServices True 
Verbose
```

You have to be careful using $PSBoundParameters directly, as you can see other things like Debug or Verbose can be added to the keys, but those are passed to internal commands as well.

---

<div class="post-metadata">

**Author:** ![hmartin](https://avatars.discourse-cdn.com/v4/letter/h/d07c76/32.png) [@hmartin](https://forums.powershell.org/u/hmartin)\
**Post date:** [May 20, 2020, 12:01pm UTC](https://forums.powershell.org/t/parameter-set-question/14403/3 "2020-05-20T12:01:50Z")

</div>

Thanks for the response. The AutoButNotRunning param is required when the IncludeExcludedServices param is specified because it’s only relevant in that context. The AutoButNotRunning param implements the filter that the IncludeExcludedServices param is un-doing. I wanted to implement it in a way that was self-documenting and so that the Syntax information was as accurate as possible. In other words, I wanted good code and I wanted to learn how to do it properly.

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [May 20, 2020, 12:39pm UTC](https://forums.powershell.org/t/parameter-set-question/14403/4 "2020-05-20T12:39:35Z")

</div>

A filter is dynamically being created and a switch is undoing something, then I’d look at that logic as well. Can you update the filter logic to append the filter based on the IncludeExcludedServices rather than manipulate the parameters. Personally have had to rethink params and logic many times, but have not had to implement dynamic params.

---

<div class="post-metadata">

**Author:** ![hmartin](https://avatars.discourse-cdn.com/v4/letter/h/d07c76/32.png) [@hmartin](https://forums.powershell.org/u/hmartin)\
**Post date:** [May 21, 2020, 8:27am UTC](https://forums.powershell.org/t/parameter-set-question/14403/5 "2020-05-21T08:27:05Z")

</div>

Thanks again. I’ll take another look.

---

<div class="post-metadata">

**Author:** ![gadivemulaphani](https://avatars.discourse-cdn.com/v4/letter/g/779978/32.png) [@gadivemulaphani](https://forums.powershell.org/u/gadivemulaphani)\
**Post date:** [May 24, 2020, 5:34am UTC](https://forums.powershell.org/t/parameter-set-question/14403/6 "2020-05-24T05:34:18Z")

</div>

please let me know which command let to find all system defined parameters.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [May 24, 2020, 5:56am UTC](https://forums.powershell.org/t/parameter-set-question/14403/7 "2020-05-24T05:56:22Z")

</div>

Please do not hijack other peoples threads. If you have a question you should create a new thread for your self.

---

<div class="post-metadata">

**Author:** ![gadivemulaphani](https://avatars.discourse-cdn.com/v4/letter/g/779978/32.png) [@gadivemulaphani](https://forums.powershell.org/u/gadivemulaphani)\
**Post date:** [May 24, 2020, 9:10am UTC](https://forums.powershell.org/t/parameter-set-question/14403/8 "2020-05-24T09:10:29Z")

</div>

how to find total number of parameters in powershell ?

how to find the total number switch, positional, optional and mandatory parameters and its names in each version of power shell?

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:30pm UTC](https://forums.powershell.org/t/parameter-set-question/14403/9 "2024-05-16T20:30:06Z")

</div>

Closed on May 16, 2024, 8:30pm UTC


