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

Question:

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:

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

How can I circumvent this and get it to work? 🙁

Answer:

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:

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.

Source:

How do I pass multiple commands to PowerShell from a shortcut file? by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply