How to loop through tasks in Windows cmd and only output task if it is NOT in a list?

I am trying to output unneeded tasks to the Windows command line. The end result should be that I basically kill all unneeded tasks with the click of a button. But to avoid any problems I am outputting the tasks names for now. I know there is other ways like disabling them on msconfig or regedit, but for now this is what I want.

The problem is I have been away from console usage for a long time and it has left me slightly confused. I have code below that outputs all running tasks, but it doesn't stop outputting the tasks in the mylist array. I think the problem is my nested for loop, but I don't know any other ways of passing the two arrays to one list so that I can check them. Any help would be greatly appreciated.

@echo off
set mylist=csrss.exe, dwm.exe, System, smss.exe Rem<<List
for /f "tokens=1" %%a in ('tasklist') do ( Rem<<This returns process name
    for %%b in ('%mylist%') do ( Rem<<b is MEANT to be each process in list
        if not %%a == %%b ( Rem<<If anything in tasklist is in mylist, don't include it
            echo %%a) Rem<<Output processes NOT in mylist.
    )
)
pause

that's much easier with the help of findstr:

@echo off
set "mylist=csrss.exe dwm.exe System smss.exe"
for /f "tokens=1" %%a in ('tasklist^|findstr /biv "%mylist%"') do echo %%a 
pause

findstr switches: - b = beginning of the line - i = ignore capitalization (optional) - v = exclude the given string(s)

whats the difference between: %%a and %variable% variables , files. If you need loop, then in most cases for has you covered. c# ile tarayıcıdan veri alıp veritabanına kaydetme Başlıklı sayfamız konusu itibarıyla inceleme yapabilirsiniz. ilginizi cekecek muhakkak birsey vardır.

What does the percent sign (% and %%) in a batch file argument , How do I see what processes are running in Windows?

Since task/image names might also contain SPACEs, I recommend not to use a SPACE-separated list of exclusions; instead I would put the items in a text file, one item per line, like this, for instance:

csrss.exe
dwm.exe
System
smss.exe

Then I would write a list of plain image names extracted from the output of tasklist into a temporary file, which I would filter by the findstr, like this:

@echo off
set "_LIST=%~1"
set "_TMPF=%TEMP%\%~n0_%RANDOM%.tmp"

> "%_TMPF%" (
    for /F delims^=^"^ eol^=^" %%P in ('
        tasklist /FO CSV /NH
    ') do @(
        echo(%%P
    )
)
findstr /VIXG:"%_LIST%" "%_TMPF%"
del "%_TMPF%"

The list file can be specified as a command line argument here.

Or you could also avoid the temporary file and use this slightly more complex code instead:

@echo off
set "_LIST=%~1"

(
    for /F delims^^^=^^^"^^^ eol^^^=^^^" %%P in ('
        tasklist /FO CSV /NH
    ') do @(
        echo(%%P^& rem/
    )
) | findstr /VIXG:"%_LIST%"

If you do want to embed the list items in the batch file, you could do it like in the following example:

@echo off
set "_TMPF=%TEMP%\%~n0_%RANDOM%.tmp"

> "%_TMPF%" (
    for /F delims^=^"^ eol^=^" %%P in ('
        tasklist /FO CSV /NH
    ') do @(
        echo ::::%%P
    )
)
for /F "delims=: eol=:" %%Q in ('
    findstr /BC:"::::" "%~f0" ^| findstr /VIXG:/ "%_TMPF%"
') do (
    echo(%%Q
)
del "%_TMPF%"

::LIST ITEMS:
::::csrss.exe
::::dwm.exe
::::System
::::smss.exe

How to run multiple commands one after another in cmd, Methods of doing iterative tasks with the Microsoft Windows XP, Vista, 7 batch file objects that can be put in the set of things that the command iterates through, the availability of wildcards, and the capability for parsing files and command output. An example that would list all the directories (but not sub-directories) on the 

View Processes Running on Windows Server – Hostway Help Center, On Windows systems, this is done via the command prompt or cmd.exe. This batch command lists tasks, including task name and process id (PID). Uses the echo off command to ensure that the commands are not shown when the code The variable i is set to 0 so that we can loop through the structure will the length of 

More Powerful Batch Files Part II - Iterating with "For", I'm trying to create a batch script to import all tasks to Windows Task Scheduler files in the directory containing the batch file (%cd%) if not [%cd%\] equ [!_dir!] CMD - SS64.com · Dir - list files and folders - Windows CMD - SS64.com For - Loop through command output - Windows CMD - SS64.com · If 

Batch Script - Quick Guide, You won't spend time relearning how to select a group of tasks (shift- clicking), pick Mac Project II 2.0 does much to get you over that hurdle very quickly, which is duration, or completion percentage using the Task Info window — but it saves The fully implemented thesaurus not only provides a list of synonyms but also 

Comments
  • There are not arrays in batch file! Also, I can't understand: if not %%a == %%b. tasklist will give you 5 columns, each one is one token (if exe name is one word, but here exe you have provided are one word.). 1st column is the image name and second column is PID. They won't be equal in NO ONE case.
  • Yes but there are some ways of making arrays in a batch file, look at this link, I am trying to basically recreate this method of outputting numbers for the .exe files tutorialspoint.com/batch_script/batch_script_arrays.htm
  • This can be called "array"?
  • there are two main errors in your code: your 2nd for should not have ' (you don't want to execute a command, but process a list); Every line of tasklist will be shown at least three times (you have four strings in your list, each filename from tasklist will be at least different from three of them. The second one is a logical error, which needs code redesign.
  • @double-beep, KieranBrown: this is not an array, but a list.
  • What I actually don't like in this approach is that you can't specify list items with spaces, but that's already a flaw of the OP's concept; I'd probably put the given process names into a text file, one name per line and use findstr's /G option; of course tokens=1 needed to be changed then...
  • TASKLIST /NH /FO CSV would allow spaces in name (but require mylist entries to be d'quoted) and also remove the header (+1)