13

I have a .cmd file which I call to open multiple instances of Command Prompt via:

launcher.cmd -fs
launcher.cmd -tds
launcher.cmd -fsd

Each command open a new command prompt.

So what I want to do is create a batch file to automatically close all the opened Command Prompt instead of manually doing it.

Rich
  • 5,417
  • 9
  • 36
  • 58
Adel Boutros
  • 9,965
  • 7
  • 51
  • 89

3 Answers3

23

Be carefull: you might kill more processes than you want:

taskkill /IM cmd.exe

You can add extra filters:

taskkill /IM cmd.exe /FI "WINDOWTITLE eq launcher*"

use

tasklist /FI "imagename eq cmd.exe " /V

to get a glimpse of what cmd.exe processes will be taskkill-ed

You could add the /F parameter to force the process to close but I would only use that if the process doesn't respond to a normal request.

rene
  • 39,748
  • 78
  • 111
  • 142
  • **taskkill** is similar to **Ctrl + C** ? – Adel Boutros Dec 29 '11 at 13:10
  • The filter on WINDOWTITLE is not working for me. I get the following error: **Information: no task in service did match the specified criteria.** – Adel Boutros Dec 29 '11 at 13:57
  • What is the window title when launcher.cmd is started? You can use the command TITLE launcher in the cmd to set the window title explicitly ( to launcher in this case). Taskkill is not similar to ctrl+c, because ctrl+c ends a script in a controlled way, taskkill simply instructs the opersting system to stop executing and unload the particular process by any means, without bothering what that process is doing. – rene Dec 29 '11 at 14:04
  • I used the command **title bla**. then I typed C:\Windows\System32\taskkill.exe /IM cmd.exe /FI "WINDOWTITLE eq bla*" but I got that error – Adel Boutros Dec 29 '11 at 14:11
  • Make sure some of your launcher.cmd are running then run "tasklist /FI "imagename eq cmd.exe" /v" to see what the windowtitle is of your launcher.cmd's – rene Dec 29 '11 at 14:20
  • To precise renes answer: The window title changes for a split second once you type in another command which is then shown in tasklist. Try running "title abc". Then open another cmd and type "tasklist /FI "imagename eq cmd.exe" /v" – JoschJava Sep 04 '19 at 15:50
0

Just a little note why accepted answer from Rene may not work. I was starting my apps from cmd file like

start "" my.exe -my -args

where my.exe was a console app and it was looking like cmd window I wanted to kill, but process name was not cmd.exe (!) and I had to use command like

taskkill /IM my.exe

So in some cases it worth to check the real process name, for example in the windows task manager.

sarh
  • 5,986
  • 4
  • 23
  • 24
-2
TASKKILL /F /IM cmd.exe /T 

good solution

demongolem
  • 9,148
  • 36
  • 86
  • 104
ant
  • 54
  • 1
  • 9