Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Named parameter in PowerShell showing as Positional

I have a simple function which has two parameters. I didn't mention them as positional but still, when I check the help for that function it is automatically assigning them a position. How can I force them to be named parameter only (don't accept value by position)?

Function Test-Params
{
    Param
    (
        [Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
        $Param1,

        [Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName)]
        $Param2
    )

    Write-Host "$Param1 $Param2"
}

When I check the help for this function or try to run the function by providing parameter positional values it works (but it shouldn't work):

Test-Params test1 test2
test1 test2

Help output:

help Test-Params -Full

NAME
    Test-Params

SYNTAX
    Test-Params [-Param1] <Object> [[-Param2] <Object>]  [<CommonParameters>]


PARAMETERS
    -Param1 <Object>

        Required?                    true
        Position?                    0
        Accept pipeline input?       true (ByValue, ByPropertyName)
        Parameter set name           (All)
        Aliases                      None
        Dynamic?                     false

    -Param2 <Object>

        Required?                    false
        Position?                    1
        Accept pipeline input?       true (ByValue, ByPropertyName)
        Parameter set name           (All)
        Aliases                      None
        Dynamic?                     false
like image 375
SavindraSingh Avatar asked Jul 12 '16 14:07

SavindraSingh


People also ask

What is positional parameter in PowerShell?

A positional parameter requires only that you type the arguments in relative order. The system then maps the first unnamed argument to the first positional parameter. The system maps the second unnamed argument to the second unnamed parameter, and so on. By default, all cmdlet parameters are named parameters.

How do you use named parameters in PowerShell?

One way to use PowerShell function parameters in a script is via parameter name -- this method is called named parameters. When calling a script or function via named parameters, use the entire name of the parameter. For example, perhaps the example param block above is stored in a PowerShell script called foo. ps1.

How do you declare a parameter in PowerShell?

The name of the parameter is preceded by a hyphen ( - ), which signals to PowerShell that the word following the hyphen is a parameter name. The parameter name and value can be separated by a space or a colon character. Some parameters do not require or accept a parameter value.

Why we use $_ in PowerShell?

The “$_” is said to be the pipeline variable in PowerShell. The “$_” variable is an alias to PowerShell's automatic variable named “$PSItem“. It has multiple use cases such as filtering an item or referring to any specific object.


1 Answers

By default, all function parameters are positional. Windows PowerShell assigns position numbers to parameters in the order in which the
parameters are declared in the function. To disable this feature, set the value of the PositionalBinding argument of the CmdletBinding attribute to $False.

Source.

So your function looks like this:

Function Test-Params
{
    [CmdletBinding(PositionalBinding=$false)]
    Param
    (
        [Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
        $Param1,

        [Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName)]
        $Param2
    )

    Write-Host "$Param1 $Param2"
}
like image 142
Martin Brandl Avatar answered Oct 23 '22 02:10

Martin Brandl