2025, Nov 10 23:00

How to Run a Python Script from PowerShell That Survives Closing the Terminal (conhost.exe Guide)

Run Python in background from PowerShell: use conhost.exe with -WindowStyle Hidden to detach the process so it keeps running after closing the Windows terminal.

Running a Python job from PowerShell that keeps going after you close the terminal sounds trivial until you try it and watch your process vanish with the window. In bash, nohup plus an ampersand solves it. In PowerShell, the equivalent requires a different approach, especially on systems where Windows Terminal is the default console.

Problem setup

On Linux, a long-running script that must outlive the terminal is typically started like this:

nohup python app_main.py &

Trying the same idea in PowerShell with a straightforward launch doesn’t work because the process stays tied to the current console session:

python app_main.py

Even attempting to delegate the launch won’t help if the child is still bound to the same terminal lifecycle:

Start-Process python app_main.py

Why it breaks in PowerShell

The crux is process attachment to the terminal host. On recent Windows setups, Windows Terminal is often the default console application. Launching a new process from PowerShell without further indirection typically opens that process in a new Windows Terminal tab, which remains a child of the terminal application. Close Windows Terminal, and the child goes with it. The missing piece is an independent console host that detaches the process tree from the terminal’s lifecycle.

The Windows equivalent of “nohup … &”

Use Start-Process via conhost.exe. The console host creates an independent console session for your process. To keep it out of sight, control the window visibility with -WindowStyle. Hidden mode covers the background requirement while ensuring the process continues after the terminal closes:

Start-Process -WindowStyle Hidden conhost.exe 'python app_main.py'

Do not add -NoNewWindow. That would tether the Python process to the current console, defeating the goal of surviving terminal closure. Launching through conhost.exe is the key difference: without it, the process can end up in a Windows Terminal tab and remain subject to termination when that terminal exits.

There are a couple of important boundaries to keep in mind. This pattern is not suitable within PowerShell remoting sessions; the launched process won’t behave as expected in that context. Also, on Unix-like systems, Start-Process does not create independent terminal windows, so you need nohup there.

Fixed background launch example

To run a Python script fully detached from the current PowerShell session and without a visible window, use:

Start-Process -WindowStyle Hidden conhost.exe 'python app_main.py'

This runs the interpreter in a separate console host. If your Python code spawns subprocesses, they inherit the same independence from the original terminal, so closing PowerShell or Windows Terminal won’t take them down.

Why this matters

Interactive shells are not reliable supervisors for long-running workloads. When the terminal application controls the lifetime of your process tree, routine actions like closing a window can interrupt jobs, corrupt work in progress, or leave partial outputs. Decoupling the job from the terminal avoids these pitfalls and gives your Python process, and any subprocesses it spawns, a stable execution environment.

Conclusion

If you need the Windows equivalent of nohup … & for Python from PowerShell, delegate to conhost.exe and hide the window with -WindowStyle Hidden. Avoid -NoNewWindow so the process does not remain tied to the current console. Remember that this approach is not for remoting scenarios, and on Unix-like systems you must still use nohup. With this setup, your background tasks continue to run cleanly and survive terminal closure.

The article is based on a question from StackOverflow by Ottpocket and an answer by mklement0.