1

I want to kill all processes with taskkill by username, with this command:

taskkill /f /fi "USERNAME eq %username%"

The problem is that I want to exclude (skip) some processes (not kill them all), such as explorer.exe, taskmgr.exe, cmd.exe, and of course current CMD instance

How can i exclude this processes with taskkill?

thanks

  • Hi there. We're not a scriptwriting service, so since you've specified that you're trying to solve this with a Batch script, please include what you've attempted already, what the results were, and what's tripping you up exactly. If this doesn't NEED to be in batch and/or using taskkill, please edit your question to remove the Batch tag, so you'll increase you chances of getting answers (that may or may not be Batch based). :) – Ƭᴇcʜιᴇ007 Sep 29 '16 at 17:07
  • the question is very clear and precise – BrianC Sep 29 '16 at 17:17
  • Brian - See below in my answer for a tested and confirmed working process from what I tested and could confirm it works as expected. Let me know how it goes. – Pimp Juice IT Sep 29 '16 at 19:27
2

Windows Native Batch Script CMD Method

Below is a batch script solution that uses Tasklist and FOR /F loops setting and parsing variables accordingly to get just the process names of the processes running of a specific user.

With Findstr these results are then parsed further to exclude any specified exclusions you set in the Exclusions variable up top.

It'll take the final remaining results and kill those process names for that specific username giving you the desired results via a batch script just as explained.

Batch Script

There are only two variables to set for this to work which are the Username and the Exclusions, and the rest will just work and do the rest of the process as you need. Just specific the full process names separated by a space one next to the other just as in the below script.

@ECHO ON
SET Username=user
SET Exclusions=explorer.exe taskmgr.exe cmd.exe 

SET tmpfl=%temp%\%~n0tmp.dat
IF EXIST "%tmpfl%" DEL /F /Q "%tmpfl%"
SET Exclusions=%Exclusions% taskkill.exe 

SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "DELIMS=: TOKENS=2" %%A IN ('TASKLIST /FI "USERNAME EQ %Username%" /FO LIST ^| FIND /I "Image name:"') DO (
    SET var=%%~A
    SET var=!var: =!
    ECHO !var! | FINDSTR /I /V "%Exclusions%">>"%tmpfl%"
)
FOR /F "USEBACKQ TOKENS=*" %%A IN ("%tmpfl%") DO (
    TASKKILL /F /FI "USERNAME eq %Username%" /IM %%~A
)
GOTO :EOF

Batch Script 2

@ECHO ON
CD /D "%~DP0"
SET Exclusions=cmd.exe explorer.exe taskmgr.exe 

SET tmpfl=%~n0tmp.dat
IF EXIST "%tmpfl%" DEL /F /Q "%tmpfl%"
SET Exclusions=%Exclusions% taskkill.exe 

SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "DELIMS=: TOKENS=2" %%A IN ('TASKLIST /FI "USERNAME EQ %Username%" /FO LIST ^| FIND /I "Image name:"') DO (
    SET var=%%~A
    SET var=!var: =!
    ECHO !var! | FINDSTR /I /V "%Exclusions%">>"%tmpfl%"
)
FOR /F "USEBACKQ TOKENS=*" %%A IN ("%tmpfl%") DO (
    TASKKILL /F /FI "USERNAME eq %Username%" /IM %%~A
)
DEL /F /Q "%tmpfl%"
GOTO :EOF

Further Resources

  • Please check the script for errors. The system can not find the file C:\Users\user1\ AppData\Local\Temp\testtmp.dat. By the way. this variable is not viable (SET Username = user) because it must serve any account. It must be replaced by %username% – BrianC Sep 29 '16 at 19:35
  • This variable (SET Username = user) not needed. Only "USERNAME EQ %username%" – BrianC Sep 29 '16 at 19:50
  • Yes. And the change is only in user logged – BrianC Sep 29 '16 at 19:53
  • I ran the script with administrative privileges on account and the same error (win7x64) – BrianC Sep 29 '16 at 19:55
  • replace %temp% with %HOMEDRIVE% – BrianC Sep 29 '16 at 20:04
2

I would personally do this by using powershell.

Something like this:

$processes = Get-Process -IncludeUserName | where {$_.UserName -like "*USERNAME*"}
$tobeignored = @("explorer","Taskmgr","cmd")
foreach($process in $processes)
{
    if($tobeignored.Contains($process.ProcessName))
    {
        continue;
    }
    else
    {
        Stop-Process $process.Id -Force
    }
}

I have not been able to test run this, I kinda dont wanna kill my own processes :) Let me know if it (doesn't) work(s)

  • Ok tested it, and made a little change :) this does the trick. Just add the proccess names (without extention) to the array, the ones u wish to leave alone – Kage Sep 29 '16 at 17:51
  • sorry i don't know anything about powershell. I need the solutions with cmd-taskkill. Thanks anyway – BrianC Sep 29 '16 at 18:04
  • Your loss :) Powershell is a powerfull scripting tool, can do a whole lot more with it compared to cmd, all the more recent (windows) OS's come with it by default – Kage Sep 29 '16 at 18:07
  • yes i know. I really appreciate your help – BrianC Sep 29 '16 at 19:26
0

You can do this by specifying multiple filters for taskkill. For example, if you want to exclude explorer.exe, add a filter for the image name not being equal to explorer.exe (with the ne operator):

taskkill /f /fi "USERNAME eq %username%" /fi "IMAGENAME ne explorer.exe"

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.