How to Write a VBS File to Kill a Task Manager Process
- 1). Create a string character to hold the value of the process you want to close. In this example, the VBS file will close the notepad application. The following code creates a string variable for the notepad process:
Dim process
process = "notepad.exe" - 2). Create an object variable that impersonates the admin user account. This is required to close processes on the machine. The following code creates a WMI object used later in the code to find the handle for the process:
Set wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\myComputer\root\cimv2") - 3). Use the WMI service variable to get the handle for the process you want to stop. The handle is a unique key given to the process that windows uses as an internal reference. To query the WMI service, use the following code:
Set process = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = " & process) - 4). Kill the process. Since the user may have several notepad windows opened, the code uses a loop to kill each process. The following code loops through each opened process and closes any of them that match "notepad.exe":
For Each objProcess in process
objProcess.Terminate()
Next - 5). Verify the process is shut down by displaying the results to the screen. The following code prints a success message for the user:
WSCript.Echo "Just killed process " & process