Batch Script That Will Kill All CMD Processes Except Itself

Posted on 2008-02-07
Medium Priority
Last Modified: 2012-06-21
I need to create a batch script that will look up all running processes of "cmd" and close/kill them. It should not kill itself though because I want to start another batch process after the kill.

I have Unix windows based tools such as ps, kill and cut. Maybe that can help.

Any assistance would be appreciated.
Question by:deharvy
LVL 12

Expert Comment

ID: 20844721
You may want to try pskill from Sysinternal

I think you need 2 batch files - first batch file kills all cmd, and the second start another process.  Otherwise, you would need to use PSID because if you use cmd.exe, all cmd will be terminated.
LVL 29

Accepted Solution

matrixnz earned 1500 total points
ID: 20846973
You could also call another program to close and then reopen cmd.exe for e.g. vbscript or autoit script, here's a simple one for AutoIT.

Download and Install AutoIT www.autoitscript.com

Copy and Paste attached code into notepad
Save as CMDKill.au3

Right Click and Select Compile to create an exe (held within the same folder as your .au3 file)
Just call it from batch or run it directly.

Change notepad.exe to your next batch file etc..

Hope that helps.

While 1
$CMD = ProcessExists('cmd.exe')
If $CMD Then
Run('notepad.exe', '')

Open in new window


Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses
Course of the Month3 days, 19 hours left to enroll

599 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question