How to Kill Multiple Processes (Tasks) by Name using VBScript?


Let’s say if you have opened quite a few tasks and you decide to close them all at once without manually close them one by one, or kill each process in Task Manager one by one.

multiple-notepad-processes How to Kill Multiple Processes (Tasks) by Name using VBScript? programming languages VBA vbscript

multiple-notepad-processes

I was coding some multi-processing application and at the testing phrases, I accidentally launch more than 30 processors of same kind (same process name). After I stopped the main host application in the Visual Studio, these processes go un-controlled (they are no longer controlled by the host application any more since I stopped the debugger before the host application kills all of them e.g. host application keeps a track of these process IDs and wait for them to finish).

How to Kill Multiple Processors (Tasks) by Name using CMD Tool TaskKill ?

These is simply using the command line tool taskkill

C:\> taskkill /F /IM notepad.exe
SUCCESS: The process "notepad.exe" with PID 23180 has been terminated.
SUCCESS: The process "notepad.exe" with PID 14632 has been terminated.
SUCCESS: The process "notepad.exe" with PID 16844 has been terminated.
SUCCESS: The process "notepad.exe" with PID 8600 has been terminated.
SUCCESS: The process "notepad.exe" with PID 6772 has been terminated.
SUCCESS: The process "notepad.exe" with PID 20080 has been terminated.

The /F stands for ‘Force’, and /IM stands for ‘Image Name’ where you can specify the name of the process. You can also use tasklist to get a list of current running processes.

                                                                               
Image Name                     PID Session Name        Session#    Mem Usage    
========================= ======== ================ =========== ============    
System Idle Process              0 Services                   0          4 K    
System                           4 Services                   0        980 K    
smss.exe                       668 Services                   0        476 K    
csrss.exe                      860 Services                   0      2,632 K    
wininit.exe                   1012 Services                   0      1,232 K    
csrss.exe                      204 Console                    1      6,976 K    
winlogon.exe                   696 Console                    1      3,384 K    
... ...
... ...

The VBScript to Kill Multiple Processors (Tasks) by Name

VBScript isn’t dead. VBScript is perfect for system administration and it has been supported every since Win98. The following VBScript function kills all processes by given Image Name.

1
2
3
4
5
6
7
8
9
10
11
'https://helloacm.com/how-to-kill-multiple-processors-tasks-by-name-using-vbscript
Sub KillAll(ProcessName)
    Dim objWMIService, colProcess
    Dim strComputer, strList, p
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
    Set colProcess = objWMIService.ExecQuery ("Select * from Win32_Process Where Name like '" & ProcessName & "'")
    For Each p in colProcess
        p.Terminate             
    Next
End Sub
'https://helloacm.com/how-to-kill-multiple-processors-tasks-by-name-using-vbscript
Sub KillAll(ProcessName)
	Dim objWMIService, colProcess
	Dim strComputer, strList, p
	strComputer = "."
	Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
	Set colProcess = objWMIService.ExecQuery ("Select * from Win32_Process Where Name like '" & ProcessName & "'")
	For Each p in colProcess
		p.Terminate				
	Next
End Sub

To use it, give it a name (case insensitive):

1
KillAll "notepad.exe"
KillAll "notepad.exe"

What it does is to execute the WMI (Windows Management Interface) Query (syntax looks like SQL),

1
Select * from Win32_Process Where Name like 'notepad.exe'
Select * from Win32_Process Where Name like 'notepad.exe'

And kill the processes all at once. Please note that this VBScript function can run not only under Windows Scripting Host (WSH), but also the VBA (Office).

Also read this post that talks about how to modify the priority of a running process using VBScript.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
636 words
Last Post: How to Unroll/Flatten Array Recursively using Javascript?
Next Post: Useful Tools for Beginners of JavaScript

The Permanent URL is: How to Kill Multiple Processes (Tasks) by Name using VBScript?

2 Comments

  1. Diwakar Sharma

Leave a Reply