Q: How can I use VBScript to stop a process if it's running?

Use VBScript to kill a process.

John Savill

September 9, 2011

1 Min Read
ITPro Today logo in a gray background | ITPro Today

A: In the FAQ “Q: How Can I Check For a Process By Using VBScript,” we looked at finding a process. Here’s how to kill it by using the process Terminate function. This also checks that the process is no longer present:

set objWMIService = GetObject ("winmgmts:")foundProc = FalseprocName = "winword.exe"for each Process in objWMIService.InstancesOf ("Win32_Process")    If StrComp(Process.Name,procName,vbTextCompare) = 0 then        foundProc = True        procID = Process.ProcessId    End IfNextIf foundProc = True Then    WScript.Echo "Found Process (" & procID & ")"    WScript.Echo "Killing Process"    Set colProcessList = objWMIService.ExecQuery("Select * from Win32_Process where ProcessId =" &  procID)    For Each objProcess in colProcessList            objProcess.Terminate()    Next    WScript.Sleep(1000) 'wait for a second    Set colProcessList = objWMIService.ExecQuery("Select * from Win32_Process where ProcessId =" &  procID)    If colProcessList.count = 0 Then        WScript.Echo "Process killed"    End IfEnd If

About the Author

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like