VBScript Scripting Techniques > Processes & Services > Processes

Kill a Process

 

Kill a Process By Name

Win32_Process
VBScript Code:
KillProc "outlook.exe"

Sub KillProc( myProcess )
'Authors: Denis St-Pierre and Rob van der Woude
'Purpose: Kills a process and waits until it is truly dead

    Dim blnRunning, colProcesses, objProcess
    blnRunning = False

    Set colProcesses = GetObject( _
                       "winmgmts:{impersonationLevel=impersonate}" _
                       ).ExecQuery( "Select * From Win32_Process", , 48 )
    For Each objProcess in colProcesses
        If LCase( myProcess ) = LCase( objProcess.Name ) Then
            ' Confirm that the process was actually running
            blnRunning = True
            ' Get exact case for the actual process name
            myProcess  = objProcess.Name
            ' Kill all instances of the process
            objProcess.Terminate()
        End If
    Next

    If blnRunning Then
        ' Wait and make sure the process is terminated.
        ' Routine written by Denis St-Pierre.
        Do Until Not blnRunning
            Set colProcesses = GetObject( _
                               "winmgmts:{impersonationLevel=impersonate}" _
                               ).ExecQuery( "Select * From Win32_Process Where Name = '" _
                             & myProcess & "'" )
            WScript.Sleep 100 'Wait for 100 MilliSeconds
            If colProcesses.Count = 0 Then 'If no more processes are running, exit loop
                blnRunning = False
            End If
        Loop
        ' Display a message
        WScript.Echo myProcess & " was terminated"
    Else
        WScript.Echo "Process """ & myProcess & """ not found"
    End If
End Sub
Requirements:
Windows version: NT 4, 2000, XP, Server 2003, or Vista
Network: any
Client software: WMI CORE 1.5 for Windows NT 4
Script Engine: WSH (replace the WScript.Echo and WScript.Sleep lines to make this work in HTAs)
Summarized: Works in Windows NT 4 or later, requires WMI CORE 1.5 for Windows NT 4.
Won't work in Windows 95, 98 or ME.
 
[Back to the top of this page]
 

Kill a Process By Window Title

JSSys3
VBScript Code:
' Close a window based on its window title ' Demo of CloseProgram() method in JSWare's JSSys3.dll
' http://www.jsware.net/jsware/scripts.php5#jssys

Option Explicit

Dim arrProcesses, arrWindowTitles
Dim i, intProcesses
Dim objJSSys
Dim strProcList

Set objJSSys = CreateObject( "JSSys3.ops" )
' Close Notepad, ask for confirmation if there are unsaved changes
objJSSys.CloseProgram "Untitled - Notepad", 1
Set objJSSys = Nothing
Requirements:
Windows version: NT 4, 2000, XP, Server 2003, or Vista
Network: any
Client software: JSSys3, VB6 Runtimes
Script Engine: any
Summarized: Works in Windows NT 4 or later, requires JSSys3 and VB6 Runtimes.
 
[Back to the top of this page]

 

 


page last uploaded: 4 March 2011, 12:56
Fileaze