0

How to close/terminate/kill an instance of a application with VBS?

I've tried the following code, suggested on several posts, and it didn´t work.

Set x =  GetObject(,"xxx.Application")
x.quit 
kalehmann
  • 4,821
  • 6
  • 26
  • 36
  • 2
    Try simple `lResult = CreateObject("WScript.Shell").Run("taskkill /f /im MyProcess.exe", 0, True)` to terminate all processes with specified name. – omegastripes Sep 08 '18 at 06:43

1 Answers1

0

I think I used WMI at one point, and that worked OK - provided the script runs with the access rights required to kill the processes in question (in other words admin rights for system processes, user rights for user context processes - I suppose there could be NT Privileges / NT Rights (system wide privileges) and custom ACL permission settings (access control list - attached to objects) involved as well - the configuration of which might vary from site to site):

Note: the below script will kill all Notepad.exe processes! (unless access is denied)

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colProcessList = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = 'Notepad.exe'")

For Each objProcess in colProcessList
    objProcess.Terminate()
Next

Some Links:

Stein Åsmul
  • 39,960
  • 25
  • 91
  • 164