简体   繁体   中英

Powershell.exe: General Question about the Syntax

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!

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, ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM