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? :(
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With