How do I make a Windows shortcut run an exe in a PowerShell window?

1

I've been recently using Blender, but it seems that some errors are only printed to the console. If I manually open PowerShell and run Blender from there, it works fine and I can read the error messages. However, I'd like to change my shortcut to do this for me.

Basically I'm trying to run an arbitrary command in PowerShell, from a shortcut. I found this question which hasn't helped; I have tried putting these in the "Target" section, to no success:

powershell "C:\Program Files\Blender Foundation\Blender\blender.exe"
powershell Invoke-Expression "C:\Program Files\Blender Foundation\Blender\blender.exe"
Invoke-Expression "C:\Program Files\Blender Foundation\Blender\blender.exe"

The first two pop open a black command window for a moment which immediately disappears, and also powershell is replaced with C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe when I hit "Apply" but I figured I'd include the shortened version here.

The third says "The name 'Invoke-Expression' specified in the Target box is not valid.".

I also came across this question, so I tried adding -NoExit -Command:

powershell -NoExit -Command "C:\Program Files\Blender Foundation\Blender\blender.exe"

This pops up a window with the message C:\Program : The term 'C:\Program' is not recognized as the name of a cmdlet, function, script file, or operable program. I find it strange that it gets stuck at the space even though I quoted the path. I also tried using a set of single quotes too:

powershell -NoExit -Command '"C:\Program Files\Blender Foundation\Blender\blender.exe"'
powershell -NoExit -Command "'C:\Program Files\Blender Foundation\Blender\blender.exe'"

This just results in a PowerShell window opening to a command prompt, printing out the command I wished to execute instead of actually executing it:

C:\Program Files\Blender Foundation\Blender\blender.exe
PS C:\Program Files\Blender Foundation\Blender>

How do I run a command / executable in PowerShell from a shortcut?

Aaron Franke

Posted 2019-10-07T06:27:11.597

Reputation: 678

Why Powershell in first choice? Why not try with Command Prompt with /K option? – Biswapriyo – 2019-10-07T06:31:59.963

I'd prefer to use the more modern tool. CMD is ancient. – Aaron Franke – 2019-10-07T06:32:58.700

1For your question, you only want to see the error message. So, the shell (cmd or powershell) does not matter in this situation. – Biswapriyo – 2019-10-07T06:36:10.260

I want to learn, not simply have my problem solved. I want to know how to use PowerShell to launch other arbitrary commands too in the future, because I know this skill will come in handy. Please only reply if you will help answer the question. – Aaron Franke – 2019-10-07T06:47:11.023

Don't feel too bad towards @Biswapriyo. We really do try to help. Most people here are not advanced users, and Powershell is an advanced tool. I too would ask the very same question because in the end, the difference will not matter, but cmd is way easier to setup. With Command Prompt, you can just make a .bat or .cmd file and it works out of the box. With PowerShell, you make a .ps1 file, then right-click it to run, or make a .cmd file to launch powershell and execute the ps1 file. It is also possible to create a shortcut to launch the ps1 file or the command, but that is even harder. – LPChip – 2019-10-07T07:17:11.193

Answers

1

You may use the following syntax:

powershell Start-Process 'C:\Program Files\Blender Foundation\Blender\blender.exe'

Start-Process executes a program, returning the process object of the application. It allows you to control the action on a file and control the environment in which the app is run. You also have the ability to wait on the process to end and subscribe to the process Exited event.

For PowerShell to use the console for output, add to the Start-Process call the parameter of -NoNewWindow, defined as: "Start the new process in the current console window. By default PowerShell opens a new window."

For more options, see the TechNet article PowerShell: Running Executables.

harrymc

Posted 2019-10-07T06:27:11.597

Reputation: 306 093

This causes PowerShell to open, and Blender to open, but it does not bind Blender to the PowerShell window or print any messages to the PowerShell window. – Aaron Franke – 2019-10-07T07:29:35.883

If my memory serves me correct, powershell is not in a path and will not start unless you provide the full path to powershell. – LPChip – 2019-10-07T07:31:17.627

@AaronFranke: Is Blender a console application? – harrymc – 2019-10-07T07:34:49.863

@LPChip: powershell.exe is in system32, so in my PATH. Perhaps you are referring to older systems. – harrymc – 2019-10-07T07:35:53.220

Yeah, it used to be in a different folder. OP did not mention they are using Windows 10, so I did not want to make that assumption. EDIT: never mind ,I see the tag now... – LPChip – 2019-10-07T10:10:38.267

@harrymc Blender is a 3D modeling program, but it prints out text if I go into the folder -> Open PowerShell window here -> ./blender.exe – Aaron Franke – 2019-10-07T16:42:41.020

Add to Start-Process the parameter of -NoNewWindow, defined as: "Start the new process in the current console window. By default PowerShell opens a new window." – harrymc – 2019-10-07T18:07:10.277

@harrymc shouldn't that last comment be edited in your answer though? – LPChip – 2019-10-07T20:49:28.527

@LPChip: Right. Done. – harrymc – 2019-10-08T08:16:03.343

The command that ended up working for me was powershell -NoExit Start-Process -NoNewWindow 'C:\Program Files\Blender Foundation\Blender\blender.exe'. The only downside is that it doesn't move the caret/cursor down, so the PowerShell window doesn't work properly if I press Enter in it. – Aaron Franke – 2019-10-15T04:03:23.870