automationScript

How to Kill process using cmd or VBScript

There might be multiple occasions when you want to kill an existing instance of a process through code.

On this page we have compiled a list of functions which can help you kill process programmatically with the help of VBScript or UFT in built methods or by using cmd window, SystemUtil(Inbuilt object in UFT/QTP), using GetObject.

For our example we have taken the process to be killed as “Excel.exe”. You can replace any of your process name in the below code snippets such as chrome.exe, msedge.exe etc.

Kill process using TASKKILL in cmd window

You can kill Excel by running a simple command in command window.

TASKKILL /F /IM EXCEL.EXE

This command can be run through vbscript like below.

Kill process using SystemUtil

SystemUtil is an inbuilt object in UFT/QTP which can interact with the underlying system. This provides a method called CloseProcessByName which kills a process by it’s name. See below how to kill excel using SystemUtil in UFT.

Find process and Kill the process

VBScript has an object to interact with the OS called Shell object. You can filter out the running process and find if Excel.exe is running in the list of processes and then kill it. See below code snippet to under stand how to find a process using shell in VBScript.

Kill the process using GetObject in VBScript

VBScript has method GetObject which can fetch an existing instance of process. We can use this to get the instance of running Excel and then kill it by calling Quit method. See below code snippet to understand how to kill excel using GetObject in VBScript.

Database connection to Excel

Checkout the code repo

Subscribe for updates