1

I want to close powershell window after my batch script completes execution. This is my batch script. I tried exit command but it is closing only command prompt.

     @ECHO OFF
     setlocal EnableDelayedExpansion

     start PowerShell.exe -Command help

     powershell.exe -command exit 

I want to close powershell window from my batchscript. Which command I am missing?

aschipfl
  • 33,626
  • 12
  • 54
  • 99
codewarrior
  • 193
  • 1
  • 4
  • 18
  • 2
    What's wrong with just `@PowerShell -Command help`? – Compo Nov 17 '19 at 20:35
  • 2
    Your last command opens a new powershell process and doesn't interact with the first one. Assuming you are using a batch/cmd you could kill the powrshell process by its PID, see: https://stackoverflow.com/questions/9486960/how-to-get-pid-of-process-just-started-from-within-a-batch-file for a few ways to do this. – Olaf Reitz Nov 17 '19 at 20:38
  • IAM actually reading the help of powershell to a file and do some processing. The powershell window should be open during this processing . Once the batch file completes this processing I want to close the powershell window at the end from the batch script. I am unable to do that. I got stuck there.I tried getting $PID and closing the window but this is not closing the powershell window. – codewarrior Nov 18 '19 at 01:47
  • If you want the batch file to wait for the PowerShell task to complete, why not invoke it directly, synchronously from your batch file? To that end, simply omit `start`. – mklement0 Nov 18 '19 at 15:47

2 Answers2

3

Kindly use below code in your CMD prompt to kill the existing PowerShell window.

taskkill /IM "powershell.exe" /F
Narayana Lvsl
  • 315
  • 2
  • 10
0

Multiple commands on the same line are separated by a SEMICOLON ;.

powershell -NoLogo -NoProfile -Command "help; exit"

Does this do what you want?

lit
  • 14,456
  • 10
  • 65
  • 119
  • Yes, but note that if you use `-Command`, the session _automatically_ exits after execution of the command(s). That is, the OP's sample command wouldn't actually keep the process alive - `-NoExit` would be needed for that. – mklement0 Nov 17 '19 at 22:32
  • I am actually copying the help of powershell to a file and doing some processing in the batch file. After all this script runs, I want to close the powershell window at the end from my script.I could close the cmd prompt by using exit but the powershell window is staying alive. I want to close powershell window . This is where I got stuck. – codewarrior Nov 18 '19 at 01:37
  • 1
    @codewarrior - What happens if you simply take off `start`? The PowerShell command will run and it will exit. – lit Nov 18 '19 at 11:22