locked
A positional parameter question RRS feed

  • Question

  • Hi there,

    I have a question about PowerShell’s positional parameters. Following are two parameter sets of Get-ChildItem cmdlet.
    (1) Get-ChildItem -LiteralPath <String[]> [[-Filter] <String>]
    (2) Get-ChildItem [[-Path] <String[]>] [[-Filter] <String>]

    For simplicity, I’ve deleted many unrelated parameters common to both parameter sets.

    And the help say -Path’s position is 0 (first value), and -Filter’s position is 1 (second value).

    Now, if I invoke the cmdlet like this: Get-ChildItem C:\Windows *.exe
    PowerShell will assign C:\Windows to -Path, and *.exe to -Filter. No problem.

    However, if I invoke a cmdlet like this: Get-ChildItem -LiteralPath C:\Windows *.exe.
    Definitely, the first parameter set will be used. -LiteralPath is a named parameter, easy, but how can *.exe be assigned to -Filter, because -Filter's position is 1, not 0. In that parameter set, there’s only one positional parameter, but it begins with 1 instead of 0.

    Can anyone explain this to me?

    Thanks in advance!

    Wednesday, September 25, 2019 1:04 PM

All replies

  • I believe it is because you are specifying a named parameter first and then passing an unnamed parameter. Since *.exe is the first unnamed parameter in the list, its positional value is actual 0 not 1

    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-6


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Wednesday, September 25, 2019 1:35 PM
  • Thanks for your prompt reply!

    But I'm afraid you didn't understand my question. Maybe I didn't wrote it clearly. I mean in the first parameter set, there's only one positional parameter, which is -Filter. Because there's only one positional parameter, it's position should be 0. Is that right? But from the help, -Filter is positional 1.

    Wednesday, September 25, 2019 2:35 PM
  • You have one named parameter and one unnamed parameter. Just because the first unnamed parameter is the second parameter you are supplying the cmdlet, doesn't mean the position is 1. I believe PowerShell is seeing the parameters and is like OK, here you are specifying a named parameter and an unnamed parameter.

    PowerShell obviously knows how to apply the named parameter, but the unnamed parameter that you supplied is the only one in the list, so it is positional parameter 0 (path) not 1 (filter).

    I can be 100% wrong, but I believe once you supply a named parameter, all other parameters should be named and not unnamed as it can cause issues exactly like you are seeing. Or you have to supply positional parameters first then named parameters


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''


    • Proposed as answer by jrv Wednesday, September 25, 2019 7:35 PM
    • Edited by clayman2 Wednesday, September 25, 2019 7:35 PM typo
    Wednesday, September 25, 2019 7:28 PM
  • I don't know how it works, but thank you very much for your answers!!
    Friday, September 27, 2019 5:31 AM
  • Just always use parameter names as it makes it easy to read and understand what you are doing

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Friday, September 27, 2019 11:56 AM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Thursday, October 3, 2019 2:17 AM