How to prevent powershell from exiting after running?

Medivh Zhu 20 Reputation points
2023-10-10T16:31:39.33+00:00

Hi there,

I'm trying to run PowerShell by Python, but my PowerShell window closes automatically after finishing, even if I add the "No-Exit".

Could anyone please help me solve this problem? Thanks a lot!

Below is my code:

import subprocess
powershell_command = 'choco update haskell-stack -NoExit'
powershell_process = subprocess.Popen(["powerShell", "-Command", "-NoExit", powershell_command], creationflags=subprocess.CREATE_NEW_CONSOLE)
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 36,401 Reputation points
    2023-10-10T16:47:32.7233333+00:00

    I'm not familiar with Python, but you need to pass the command text after the -Command switch.

    import subprocess
    powershell_command = 'choco update haskell-stack'
    powershell_process = subprocess.Popen(["powerShell", "-NoExit", "-Command", powershell_command], creationflags=subprocess.CREATE_NEW_CONSOLE)
    
    
    

    You might also need to put brackets {} around the command text.

    powershell_command = '{choco update haskell-stack}'
    
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.