Search code examples
windowsbatch-filetaskkill

Windows TASKKILL syntax issue


After all these years, you'd think I had done some Windows batch files of some type. But you'd be wrong. I am updating a batch file and encountered the following statement:

TASKKILL /F /FI "WINDOWTITLE eq SIM RH*" /IM perl.exe

Why both the /FI and the /IM? Are the two clauses anded together? If I leave off the /IM, the correct processes get killed.

The only way this makes sense is if they get ended and kill only tasks with that window title AND are perl.exe tasks.


Solution

  • Yes they are added!

    /IM would be killing all tasks with different process-IDs
    /FI applies a filter to that. You can test that with something like this:

    @echo off
    for /L %%i in (1,1,5) do start "%%i" cmd.exe
    timeout /t 3
    taskkill /F /FI "WindowName ne 3" /IM cmd.exe
    

    This should leave you with 1 command-prompt with the title 3. All others will be closed.

    Changing the ne to eq would only close the command-prompt with the title 3 leaving you with 1, 2, 4, 5 and yourBatch.bat