1

if i am opening powershell via cmd with following syntax:

powershell "C:\Users\MyUser\Downloads\Myscript.ps1"

and this is the syntax:

PowerShell[.exe]
    [-PSConsoleFile <file> | -Version <version>]
    [-NoLogo]
    [-NoExit]
    [-Sta]
    [-Mta]
    [-NoProfile]
    [-NonInteractive]
    [-InputFormat {Text | XML}]
    [-OutputFormat {Text | XML}]
    [-WindowStyle <style>]
    [-EncodedCommand <Base64EncodedCommand>]
    [-ConfigurationName <string>]
    [-File - | <filePath> <args>]
    [-ExecutionPolicy <ExecutionPolicy>]
    [-Command - | { <script-block> [-args <arg-array>] }
                | { <string> [<CommandParameters>] } ]

PowerShell[.exe] -Help | -? | /?

microsoft docs

which paramater am i refering to
is it "[-PSConsoleFile <file> | -Version <version>]"?
because in my example i did not specify the parameter with "-" and it is still working!

Mofi
  • 46,139
  • 17
  • 80
  • 143
NoErrorNoCry
  • 55
  • 1
  • 7
  • 1
    it's called a *Positional Parameter*. If the parameter arguments align where the parameters would go, it will work due to it's position. You can recreate it yourself using metadata in a parameter declaration inside a function: `[parameter(Position=0)]`, etc. – Abraham Zinala Jun 08 '22 at 20:34
  • so -command is in position [0] ? if i had to guess i would say -PSconsoleFile is in position [0] – NoErrorNoCry Jun 08 '22 at 20:44
  • See mklements answer – Abraham Zinala Jun 08 '22 at 20:48

1 Answers1

3

It cannot be inferred from the syntax diagram you quote from the linked help topic, but the first positional (unnamed) argument you pass to the PowerShell CLI implicitly binds to:

  • -Command in powershell.exe (Windows PowerShell)

  • -File in pwsh (PowerShell (Core) 7+)

(It is unfortunate that the CLI's syntax diagram doesn't contain this information (in cmdlet help topics, it does, though it may not be readily obvious - see this answer); it also doesn't reflect which parameters are mutually exclusive, such as -File and -Command. As of this writing, the information about the positional parameters isn't even in the description at about_PowerShell_exe, but it is in about_Pwsh)

In other words:

powershell "C:\Users\MyUser\Downloads\Myscript.ps1"

is the same as:

powershell -Command "C:\Users\MyUser\Downloads\Myscript.ps1"
  • That is, the script path is passed positionally in the first command, i.e. its target parameter isn't explicitly specified the way it is in the second command. Another way of looking at is that the path was passed as an unnamed argument.

  • Order matters among positional arguments, and the target command must (a) designate each parameter that may be bound positionally as such and (b) specify a number that defines the relative position among all positional arguments. (In PowerShell code, an advanced function or script (cmdlet-like) would express that with a parameter attribute such as [Parameter(Position=0)], as Abraham Zinala notes) - see the conceptual about_Functions_Advanced_Parameters help topic).

  • In the PowerShell CLI, special considerations apply to positional arguments:

    • Any arguments following the -Command or -File argument are treated as pass-through arguments:

      • With -File, they are passed to the specified .ps1 script.
      • With -Command, they form part of the PowerShell source code that is being passed (if multiple arguments are passed, they are individually stripped of syntactic " quotes and joined with spaces).
    • Therefore, -Command and -File - as well any pass-through arguments - should always be placed last on the CLI command line.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • 1
    huh, that is odd 'cause I know I've seen it be `-command` before as the first parameter. – Abraham Zinala Jun 08 '22 at 20:39
  • 2
    @AbrahamZinala, `-Command` is the implied default for `powershell.exe`, but you may specify it explicitly; am I misunderstanding your comment? – mklement0 Jun 08 '22 at 20:57
  • thanks for that neat explanation - shouldn't the syntax diagram list the "-command" parameter first to express the position [0]? sorry if i have asked something obvious now, but that was a little bit complex – NoErrorNoCry Jun 08 '22 at 21:04
  • 1
    @NoErrorNoCry Yes, the syntax diagram is inadequate - I've added more details to the answer. For `pwsh` the equivalent help topic now mentions that `-File` is the default, but one you link to, for `powershell.exe`, does _not_ mention that `-Command` is the default. – mklement0 Jun 08 '22 at 21:07
  • 1
    @NoErrorNoCry, you could raise an issue at  https://github.com/MicrosoftDocs/PowerShell-Docs/issues – mklement0 Jun 08 '22 at 21:10