WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

WMI
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Close All but One Instance of an Application

 Keywords: Close all one instance specific app applocation process session id  WMI  

;***************************************************************************
;**
;**  Close all but one instance of a specific app in a specific session id using WMI
;**
;**
;** Purpose: Close all but one instance of a specific app in a specific session id
;** Inputs:
;** Outputs:
;** Revisions: Deana Falk 2014.05.06
;***************************************************************************

;***************************************************************************
;**
;**  Close all but one instance of a specific app in a specific session id
;**  uses WMI
;**
;***************************************************************************
Run('notepad','')
Run('notepad','')
appname = "notepad.exe" ; root filename of the process

objWMIService = ObjectGet("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
strQuery = `SELECT * FROM Win32_Process WHERE SessionId=1 AND Name = "`:appname:`"`
colProcesses = objWMIService.ExecQuery( strQuery )
mycount = colProcesses.Count
ForEach objProcess In colProcesses
    If mycount == 1 Then Break
    ;Message("Process ":objProcess.name, "Process Id ":objProcess.ProcessId:" Session ID ":objProcess.SessionId)
    objProcess.Terminate(0)
    mycount = mycount-1
Next
colProcesses  = 0
objWMIService = 0
Exit

Article ID:   W18466
Filename:   Closw All but One Instance.txt
File Created: 2014:05:06:12:50:22
Last Updated: 2014:05:06:12:50:22