Development‎ > ‎VBScript‎ > ‎

Terminate Process by name

While writting VBScript utilities it is sometimes desirable to make sure that certain processes
are not running before the script code executes.

Following code will kill all processes with the name passed to the subroutine in the input parameter.

'********************************************************************************
'* Sub signature: CloseProcessByName(sProcessName)
'* Description: Closes all processes with the process name matching sProcessName
'*                parameter. If more then one process with that name is found
'*                subroutine will terminate all instances. 
'* Parameters
'*   sProcessName - name of the process to terminate (e.g. iexplore.exe, excel.exe)
'*
'********************************************************************************

Sub CloseProcessByName(sProcessName)
    Dim oWMIService
    Set oWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")

    'Get the process based upon the Parameter
    Dim oProcessList : Set oProcessList = oWMIService.ExecQuery _
        ("SELECT * FROM Win32_Process WHERE Name = '" & sProcessName &"'")

    Dim oProcess
    For Each oProcess in oProcessList
        oProcess.Terminate()
    Next

    'Clean up
    Set oProcess = Nothing
    Set oProcessList = Nothing
    Set oWMIService = Nothing
End Sub

Whas that article helpfull ?

Comments