Kill a process from Task Manager

Today, one of my colleagues asking me for a script to kill a process from a computer’s Task Manager.
Below is the coding that kill a process – “calc.exe” from a Task Manager of a computer named “ABC-D-1234”. Thus, to test the script, you can launch a calculater in the computer first before running the script.
KillWin32Process “ABC-D-1234”, “calc.exe” 

Sub KillWin32Process(vComputer, vProcessName)
    Dim objWMIService
    Dim strWQL
    Dim colProcessSet
    Dim objProcess
    On Error Resume Next

    Set objWMIService = GetObject(“winmgmts:\\” & vComputer & “\root\cimv2”)
    If Err <> 0 Then
        Wscript.Echo “Connect to ” & vComputer & ” failed.”
        Exit Sub
    End If

    strWQL = “SELECT * FROM Win32_Process”
    strWQL = strWQL & ” WHERE Name='” & vProcessName & “‘”
    Set colProcessSet = objWMIService.ExecQuery(strWQL)

    If ColProcessSet.Count > 0 Then
        For Each objProcess In colProcessSet
            objProcess.Terminate() ‘Kill Process
            If Err = 0 Then
                Wscript.Echo “Kill ” & vProcessName & ” successful.”
                Wscript.Echo “Kill ” & vProcessName & ” failed.”
            End IF
        Wscript.Echo vProcessName & ” not found.”
    End If
    Set colProcessSet = Nothing
    Set objWMIService = Nothing
End Sub
To use it, copy the script to a notepad and name it as <name>.vbs. Then, run it from the command prompt using this command: cscript <name>.vbs.


About this entry