Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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? :(

like image 458
emboel Avatar asked Jun 08 '18 17:06

emboel


People also ask

How do I run multiple PowerShell commands in one script?

The commands can be in one PowerShell invocation by using a SEMICOLON at the end of each line. You need just one execution of PowerShell, and multiple commands passed to it.

Can you run batch commands in PowerShell?

To run the batch commands from PowerShell, we can use the Start-Process cmdlet as earlier explained and which executes a cmd command on the server. The above command creates directory cmddir on the local server.


1 Answers

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.

like image 144
mklement0 Avatar answered Sep 20 '22 09:09

mklement0