Windows batch file - taskkill if the window title contains text

I want to write a simple batch file to kill a process that contains certain text in the window title. Right now I have:

taskkill /fi "Windowtitle eq XXXX*" /im cmd.exe 

And it works, except what I want to do is use a wildcard at both the beginning and the end of the header. So something like:

 taskkill /fi "Windowtitle eq \*X*" /im cmd.exe 

But I tried this and it does not work. Is there something that I am missing or is it impossible?

+8
windows cmd process batch-file taskkill
source share
2 answers

No, wildcards are not allowed at the beginning of a filter.

 for /f "tokens=2 delims=," %%a in (' tasklist /fi "imagename eq cmd.exe" /v /fo:csv /nh ^| findstr /r /c:".*X[^,]*$" ') do taskkill /pid %%a 

This will result in a list of tasks in csv and verbose format (which will include the window title as the last field in the output).

The list is filtered by findstr with a regular expression that will search for the specified text ( X ) in the last field.

If any line matches the filter, for will marx it, extracting the second field (PID), which will be used in taskkill to complete the process.

+5
source share

In a special case, you yourself launched a command window from a batch file, you can specify the window title using the command

 START MyWindowTitle c:/MyProcess.exe 

Thus, it is easy to kill a process using only

 taskkill /fi "WindowTitle eq MyWindowTitle" 
+4
source share

All Articles