简体   繁体   中英

How do I pass multiple commands to PowerShell from a shortcut file?

Simple wish for a Windows shortcut: I want to open a PowerShell window in a specific directory, and then have the shortcut enter and run a command.

This is how it looks right now when editing the "Target" of the shortcut:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -command "cd 'C:\eARKIV\Programmer\Android ADB'" -Command 'test'

The directory change works, but I get the below error when trying to enter an input through the shortcut:

"Set-Location : A parameter cannot be found that matches parameter name 'Command'."

How can I circumvent this and get it to work? :(

Only one -Command argument is supported; everything after (the first) -Command becomes part of the command to execute in the new session [1] , as powershell -? explains .

To pass multiple commands, use ; inside the "..." string passed to the (one and only)
-Command parameter:

... -NoExit -Command "cd 'C:\eARKIV\Programmer\Android ADB'; & 'test'"

Note that -Command must be the last argument passed. [2]


[1] Therefore, -Command 'test' accidentally became additional arguments passed to your cd ( Set-Location ) command inside the new PowerShell session, and that's what the error complained about - which also implies that the cd command did not succeed in changing the current location (working directory)

[2] Technically, you may follow -Command with multiple arguments, but they all become part of the code that PowerShell executes in the new session. For conceptual clarity and to avoid (more severe) escaping and quoting headaches, it is preferable to pass all of the commands as a single , "..." -quoted string.

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