VBScript to Terminate a Windows Process
April 10th, 2009 | Published in Programming, technology, Web Development
I needed a quick vbscript to terminate a specific windows process that seemed to hang. This is the script that will do it. It uses SQL to lookup the process name from the win32_process list, then uses the terminate function to close it.
NOTE: this vbs to terminate a windows process was only tested on Windows XP professional edition.
Just leave strComputer variable as-is if you are using it on the same machine as the process you want terminated. Replace the ENTER_PROCESS_NAME_HERE portion of the code with your process name. (to find a windows process name, just go to the windows task manager, click “processes” and the name that appears in the “process name” column is the string you should enter.)
1 2 3 4 5 6 7 8 9 | strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colProcessList = objWMIService.ExecQuery _ ("SELECT * FROM Win32_Process WHERE Name = 'ENTER_PROCESS_NAME_HERE'") For Each objProcess in colProcessList objProcess.Terminate() Next |
The for loop ensures that if there are multiple processes open that match your query, all of them get terminated. If you are looking to terminate a specific process, make sure you enter it’s full name, and make sure you research other processes so that you don’t terminate other processes that you didn’t intend to.
If you want to terminate multiple, but similar processes, you can use wildcard, just like in any sql statement.
1 | SELECT * FROM Win32_Process WHERE Name LIKE 'm%' |
This code will select all processes beginning with the letter M, for example.
Hope this helps. Comments welcome.