2

I'm trying to run a command that uses a variable as the executable. In Cmd, I would just do the following:

%MY_PROGRAM% -my_program_option

I'm trying to do the same in PowerShell doing this, but it always launches in a new command (not PowerShell) terminal:

Invoke-Expression "${MY_PROGRAM} -my_program_option"

How can I make it launch in the current PowerShell terminal session?

halfer
  • 19,824
  • 17
  • 99
  • 186
Tricky
  • 3,791
  • 3
  • 10
  • 23
  • Try `. "${MY_PROGRAM} -my_program_option"` or `& "${MY_PROGRAM} -my_program_option"`. Seems [already answered here](https://stackoverflow.com/questions/24940243/running-cmd-command-in-powershell?rq=1). – Zilog80 May 04 '21 at 13:48
  • @Zilog80, you cannot pass an entire command line to `.` or `&`, only a command _name or path_; arguments must be passed separately. – mklement0 May 04 '21 at 13:54
  • 1
    @mklement0 You're right, my bad, i was lazzi. – Zilog80 May 04 '21 at 13:55
  • Hi Tricky. Could I trouble you to use a spell-checker? I am noticing that your posts are normally incorrectly spelled, and they have been corrected enough times now to suggest you are not wilfully not taking care. It's worth noting that while volunteer editors are on hand to improve people's work here, there are not enough of us to go around, and we don't have the resources to clean up after everyone. Have mercy on us! – halfer May 21 '21 at 21:04

1 Answers1

2
& ${MY_PROGRAM} -my_program_option

If ${MY_PROGRAM} refers to a console application, it should:

  • execute in the same window
  • synchronously (i.e. PowerShell will wait until the application exits before continuing)
  • with the application's output streams connected to PowerShell's streams, which enables directly collecting or redirecting the application's output.

As for what makes an application a console application: a flag in a given executable indicates whether it is a console-subsystem or a GUI-subsystem executable (note that these are Windows-only concepts).

Behavioral implications:

  • If a given executable is a console-subsystem executable, PowerShell executes it in the same window, in the manner described above.

    • Note: This also applies to directly invoked script files that are associated with console-subsystem executables, assuming their filename extensions are listed in the $env:PATHEXT environment variable so as to allow their direct execution; e.g., .cmd or .bat.
  • By contrast, a GUI-subsystem executable launches asynchronously - with no further connection to the calling session - and it is free to create its own GUI, including potentially creating a new console window, or even no UI at all.

    • You can use Start-Process if you need more control over calling such applications.
mklement0
  • 382,024
  • 64
  • 607
  • 775
  • Im my case, it launches its own TCL console. But it always opens in a separate command window. Is it the application's fault? – Tricky May 04 '21 at 13:57
  • @Tricky Can you give us what you have in `%MY_PROGRAM%` ? – Zilog80 May 04 '21 at 13:58
  • `"${env:VIVADO20.2}" -mode tcl` returns `C:\Xilinx\Vivado\2020.2\bin\vivado` . It might be the program, as launching it directly just gives me a command window, whereas another program just runs locally. – Tricky May 04 '21 at 14:00
  • @Tricky, yes, it sounds like your specific executable is the problem - please see my update. – mklement0 May 04 '21 at 14:48