PowerShell / PowerShell/PowerShell

ParameterSet Binding Error in PS5.0+

Open
#2,212 5 comments 0 reactions 1 assignee View on GitHub

@powercode is already working on this.

Since Feb 13, 2025.

In-PR KeepOpen Needs-Investigation WG-Engine
Dominant language
C#
Stars
55.5k
Forks
8.5k
Avg merge
1d 2h
Merged PRs (30d)
88

Description

When binding with multiple parameter sets, the binder confuses position and sets the wrong value on the parameter.

This bug exists since at least PS 4

Steps to reproduce

function Test-Parameter {
    [CmdletBinding()]
    param
    (
        [Parameter(ParameterSetName = 'Two', Position = 0)]
        [Parameter(ParameterSetName = 'One', Position = 1)]
        [string]
        $First,

        [Parameter(ParameterSetName = 'Two', Position = 1)]
        [string]
        $Second
    )

    "ParameterSet " + $PSCmdlet.ParameterSetName
    $PSBoundParameters
}

Test-Parameter Hello World

Expected behavior

The expected output is:

ParameterSet Two

Key    Value
---    -----
First  Hello
Second  World

Actual behavior

The $First parameter is incorrectly bound to the same value as the $Second parameter:

ParameterSet Two

Key    Value
---    -----
First  World
Second World

Environment data

This happens on PowerShell 4, 5, and 6.0.0-alpha.9

Experimental Notes

The bug is caused by having the Position index of the first parameter in the non-matched parameter set be the same as the Position index of the second parameter in the matched parameter set.

That is, if you change [Parameter(ParameterSetName = 'One', Position = 1)] to [Parameter(ParameterSetName = 'One', Position = 0)] it is does not occur

And if you change [Parameter(ParameterSetName = 'Two', Position = 1)] to [Parameter(ParameterSetName = 'Two', Position = 10)] it does not occur

But if you change them both to, say, Position = 5 then it does occur. Even if there are other parameters and other parameter sets.

And of course, if you specify the parameter name when you call it, or if the parameters are of different types, then this does not occur.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.