Unable to taskill some cmd.exe instances

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Unable to taskill some cmd.exe instances

#1 Post by DOSadnie » 02 Aug 2022 09:23

Long story short: by a mistake a created a BAT file with a looped execution of itself. This resulted in multiple CMD icons appearing extremely fast on and disappearing from my Taskbar


So instead of just resetting the system I went to

Task Manger > Process

and closed Windows Command Processor. But it did not work, as it instantly was right back. So I quickly wrote this

Code: Select all

taskkill cmd.exe /F
script and then very quickly executed it multiple times with a left mouse button clicks of BAT file holding it. And it worked, i.e. the Taskbar was clear of CMD icons and Task Manger was also showing no signs of Command Prompt


So then I wanted to be sure that this BAT will work always. So I executed

C:\Windows\system32\cmd.exe

both normally and As Administrator and once again executed my newest BAT. And [surprise] it did not work. So I went to

Task Manger > Details > cmd.exe > [RIGHT CLICK] End Task

and [surprise, surprise] it also did not work. But when I tried

Task Manger > Details > cmd.exe > [RIGHT CLICK] End Process Tree

it did work. The same case was with the other cmd.exe instance. And so I went back to my BAT and reworked it to

Code: Select all

taskkill /F /IM /T cmd.exe
but [surprise, surprise, surprise] it did not work


I also repeated some time later my test- and to my [this time real] surprise End Task in Task Manger was able to close without a sweat all of my cmd.exe instances. But nevertheless that BAT file still was not able to do so



So how can I 100% taskkill every instance of cmd.exe - or - is there an alternative command for BAT scripts for closing of programs? Or maybe some PS1 script could take care of this issue? [A PowerShell script could be even better as it could execute taskkill or similar command multiple time, with some time delays in-between]



I am using Windows 10 Enterprise 20H2 19042.746 x64
Last edited by DOSadnie on 02 Aug 2022 10:15, edited 1 time in total.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Unable to taskill some cmd.exe instances

#2 Post by aGerman » 02 Aug 2022 10:14

DOSadnie wrote:
02 Aug 2022 09:23
taskkill /F /IM /T cmd.exe
I guess you just messed with the arguments. /IM is for image name and apparently cmd.exe is the imagename. Thus, it must follow after /IM rather than after /T.

Steffen

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Unable to taskill some cmd.exe instances

#3 Post by ShadowThief » 02 Aug 2022 15:04

Take a look at the output of taskkill /?

Code: Select all

TASKKILL [/S system [/U username [/P [password]]]]
         { [/FI filter] [/PID processid | /IM imagename] } [/T] [/F]
/IM must be immediately followed by the imagename, and /T is separate from it

DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Unable to taskill some cmd.exe instances

#4 Post by DOSadnie » 03 Aug 2022 13:14

ShadowThief wrote:
02 Aug 2022 15:04
Take a look at the output of taskkill /?
[...]
I had. But being molto stupido that I am, I then ignored everything other than that nicely spaced list of parameters


So this works

Code: Select all

taskkill /IM cmd.exe /T /F
although with one caveat - it is better to run this BAT in the As Administrator mode, so that also elevated CMDs get closed


Thank you both for making me learn something today

Post Reply