Home » MSI (Windows Installer) » Useful Scripts for Custom Actions » Kill One or Many Processes

Kill One or Many Processes

Posted on by

Description:

Useful when we want to terminate processes during uninstall.  Sometimes when processes are still running during uninstall it may cause the uninstall to fail or not remove all files. This script is an example of how to kill one or many processes.

Source:

NA

Script:

Using Windows Management Instrumentation:

Dim process, processArray, objWMIService, sQuery, objProcess, objProcessList

'Add one or many processes to this array
processArray = Array("process1.exe", "process2.exe", "process3.exe")

For Each process In processArray    

	set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
	sQuery="select * from win32_process where name='" & process & "'"
	set objProcessList = objWMIService.execquery(sQuery)

	Do While objProcessList.count > 0
		For Each objProcess in objProcessList
			If IsObject(objProcess) Then
				objProcess.Terminate()
			End If
		Next

		set objWMIService =getobject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
		sQuery="select * from win32_process where name='" & process & "'"
		set objProcessList = objWMIService.execquery(sQuery)		
	Loop

Next

set objWMIService= nothing
set objProcessList =nothing

 

Or using Windows Script Host:

dim WshShell : Set WshShell = CreateObject("WScript.Shell")

'Add one or many processes to this array
dim processArray  : processArray = Array("notepad.exe", "iexplore.exe")
dim process
 
For Each process In processArray  
	WshShell.Run "TASKKILL /im " & chr(34) & process & chr(34) &  " /f /t", 0, true
Next
 
Set WshShell = Nothing

 

Comments have now been disabled. If you have a question to ask about this post please ask the community!