Parameter Position Attribute issue in V5.0 PowerShell

This topic contains 3 replies, has 3 voices, and was last updated by  Jonatas Mendes 5 months, 2 weeks ago.

  • Author
    Posts
  • #69876

    Jonatas Mendes
    Participant

    Hi Folks. First I would like to thank you all for the community. I'm from Brazil and become a really PowerShell fan. I found a issue on Version 5.0 when look to Parameter Position Attribute. When I look for help using get-help about_parameters I receive the sample below:

    Parameter Position?
    This setting indicates whether you can supply a parameter's value
    without preceding it with the parameter name. If set to "0" or "named,"
    a parameter name is required. This type of parameter is referred to as
    a named parameter. A named parameter can be listed in any position
    after the cmdlet name.

    If the "Parameter position?" setting is set to an integer other than 0,
    the parameter name is not required. This type of parameter is referred
    to as a positional parameter, and the number indicates the position
    in which the parameter must appear in relation to other positional
    parameters. If you include the parameter name for a positional
    parameter, the parameter can be listed in any position after the
    cmdlet name.

    For example, the Get-ChildItem cmdlet has Path and Exclude parameters.
    The "Parameter position?" setting for Path is 1, which means that it
    is a positional parameter. The "Parameter position?" setting for Exclud
    is 0, which means that it is a named parameter.

    This means that Path does not require the parameter name, but its
    parameter value must be the first or only unnamed parameter value
    in the command. However, because the Exclude parameter is a named
    parameter, you can place it in any position in the command.

    As a result of the "Parameter position?" settings for these two
    parameters, you can use any of the following commands:

    Get-ChildItem -path c:\techdocs -exclude *.ppt
    Get-ChildItem c:\techdocs -exclude *.ppt
    Get-ChildItem -exclude *.ppt -path c:\techdocs
    Get-ChildItem -exclude *.ppt c:\techdocs

    If you were to include another positional parameter without including
    the parameter name, that parameter would have to be placed in the order
    specified by the "Parameter position?" setting.

    When I look for help of Get-ChildItem -Full the parameter -Path had the value of 0 instead of 1. I found the same issue in all others cmdlets help files, like get-acl, get-content and others. As the output said before, when you have a 0 value, that means you gonna have to use the parameter name. Does anyone know Why this? Should we contact Microsoft to Update the Help or change the PowerShell dll?

    Sorry if my text could be a little confuse. Every body feel free to correct me and help me out improve my English.

    Thx.

  • #69912

    Rob Simmers
    Participant

    Positional parameters are used if you don't provide a specific named parameter, you're specifying the parameter order. Best practice, you should always use a named parameter versus assuming you have the right order. The below is example walks through some different scenarios, but the last example would be incorrect because you are passing a last name to the first name positional parameter, but the command will execute regardless:

    
    function Test-It {
        param (
            [Parameter(Position=0)]
            [string]$FirstName,
            [Parameter(Position=1)]
            [string]$LastName
        )
        begin{}
        process{
            #Note that this string format is positional as well with zero {0} as the first placeholder
            $result = "My First Name is {0} and last name is {1}" -f $FirstName, $LastName
        }
        end{
            $result
        }
    }
    
    "`r`n**Named Parameter"
    Test-It -FirstName "John" -LastName "Smith"
    "`r`n**Positional parameter"
    Test-It "Sally" "Thompson"
    "`r`n**Named Parameter backwards"
    Test-It -LastName "Thompson" -Firstname "Sally"
    "`r`n**Positional parameter backwards"
    Test-It "Smith" "John"
    

    Output:

    **Named Parameter
    My First Name is John and last name is Smith
    
    **Positional parameter
    My First Name is Sally and last name is Thompson
    
    **Named Parameter backwards
    My First Name is Sally and last name is Thompson
    
    **Positional parameter backwards
    My First Name is Smith and last name is John
    
  • #69913

    Richard Siddaway
    Moderator

    I've blogged about this at

    • #69924

      Jonatas Mendes
      Participant

      Thank you very much Richard Siddaway. I'm using your book PowerShell in Depth Second Edition to study. I'll create a portuguese course about PowerShell.

      Thank's Rob Simmers too. You help me out to get more examples for my course.

You must be logged in to reply to this topic.