Single parameter argument with position equal to 1 or no argument value?

Viewed 103

I have stumbled upon a function with single parameter, and there are 2 things I do not understand simply because documentation does not mention them.

Here is a function:

function Some-Function
{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory,
        Position = 1, ValueFromPipeline)]
        [string] $Input
    )
}

Question 1:

Since this is single parameter function, why is Position set to 1? what does that mean? I don't see any point here, what is wrong with the default value of Position = 0?

Question 2:

ValueFromPipeline and Mandatory arguments are not set to any value ie, = $true or = $false what is the default value of these arguments if not set to value?

I can't find anywhere in the docs to explain this, and none of the examples on docs are like this one.

1 Answers

Since this is single parameter function, why is Position set to 1?

There is no strict need to set a Position property at all in this case.

  • While it is a good convention to start Position properties with 0, it isn't technically necessary - all that matters is the relative ordering of all parameters with Position attributes.

  • In the absence of any Position properties, it is the order of parameter declarations (other than [switch] parameters) that implicitly defines their positional ordering, EXCEPT if you use [CmdletBinding(PositionalBinding=$false)], in which case only explicit Position properties matter.

Note: The presence of [CmdletBinding(PositionalBinding=$false)] or at least one [Parameter(Position=...)] attribute implicitly switch the default to non-positional: that is, all parameters that do not explicitly specify a Position value become non-positional, meaning they can only be passed arguments if preceded by the parameter's name (e.g., -foo bar instead of just bar).


ValueFromPipeline and Mandatory arguments are not set to any value

In PowerShell v3+, omitting an attribute's property value defaults to $true, in the interest of brevity.

Related