Batch script to close all open Command Prompt windows
Asked Answered
S

3

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.

Saunders answered 29/12, 2011 at 11:19 Comment(0)
B
26

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.

Bluey answered 29/12, 2011 at 11:36 Comment(6)
taskkill is similar to Ctrl + C ?Saunders
The filter on WINDOWTITLE is not working for me. I get the following error: Information: no task in service did match the specified criteria.Saunders
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.Bluey
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 errorSaunders
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'sBluey
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"Arran
C
1

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.

Crane answered 10/2, 2021 at 8:56 Comment(0)
B
-2
TASKKILL /F /IM cmd.exe /T 

good solution

Barden answered 6/6, 2014 at 18:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.