I'm on Windows 11. I need to run a program with Powershell. This program has its own UI. I want to be able to close the Powershell window without the program closing. But no matter what I do, as soon as I close the Powershell window, the program closes. Even using Start-Process to run the program, it still closes when the Powershell window is closed.
If I use Powershell to run Powershell, open the program within that Powershell using Start-Process, then Exit that Powershell, it stays open. But as soon as I close the original Powershell window, the program closes.
Use powershell.exe
, the Windows PowerShell CLI, to non-interactively to launch your program, e.g.:
powershell -NoProfile { & "$env:LOCALAPPDATA\Programs\Microsoft VS Code\Code.exe" }
Your symptom implies that your GUI application explicitly attaches to its caller's console, which is atypical for GUI applications; however, it is sometimes used to provide diagnostic output to the console.
Visual Studio Code is an example of such an application, if you launch its .exe
file directly (whereas by default it launches via an aux. batch file, code.cmd
), as shown above (assumes a user-level installation of Visual Code).
Once attached to the caller's console, closing the latter also terminates the launched application, which is what you saw.
I'm hazy on the details, but I presume that the reason the above works is:
PowerShell launches GUI(-subsystem) executables asynchronously (even in direct invocation, via &
, as shown above - no need for Start-Process
for simple launching).
By the time the asynchronously launched application tries to attach to its parent process' console, the parent process no longer exists (because the CLI process terminated right after the asynchronous launch was initiated), so no dependency on another process('s console) is created.