WMI – Stop Process

VBScript Kill Process

If ever you wish to stop or terminate a Windows process, then this is the page for you.  Before you begin killing processes, you may wish to list processes running on a the Windows Server 2003 or XP computer.  Task Manager is a great utility to match the names of the programs with their processes, you would not want to inadvertently kill the wrong process!

Topics for VBScript Kill Process

 ♦

Scenario – Why you would want to Terminate a Process?WMI Win32_Process kill terminate

Perhaps you wish to restart a process, if so, then obviously you need to stop the process before you can start it again.  Before the WMI script, can stop the program you need to know the precise name of the corresponding program.  One way to investigate the names would be to Launch Task Manager, select the Application tab, right-click the Task and then choose, Go to Process. Examples of processes that you could terminate include, spoolsv.exe, outlook.exe.

Another reason why you may wish to investigate, then kill processes is if a virus manages to launch itself as a process.  Once you spot the impostor, then the next step is to create a WMI script, which terminates that virus \ process.

Example 1 – WMI Script to Terminate a Process on the Local Machine

The purpose of this script is to terminate a process on the local Windows machine.  Think of this script as a preliminary script leading the main event in Example 2.

Prerequisites for Your WMI Stop Process Script

Run this script on Windows Server 2003 or XP. Naturally, if the named process does not exist, there is nothing for the script to terminate.  Therefore, you need to start the process referenced on line 9, in my example this process (program) is calc.exe.  Consider running my StartProcessScript first.

Note the .terminate method does not work with NT 4.0 or Windows 9x machines.

Instructions for VBScript to Terminating a Process

  1. Copy and paste the example script below into notepad or a VBScript editor.
  2. Save the file with a .vbs extension, for example: ProcessKill.vbs 
  3. Double click ProcessKill.vbs and check Task Manger, Application Tab.  You may actually wish to start both Calc.exe and Task manager before your run the script.

‘ ProcessKillLocal.vbs
‘ Sample VBScript to kill a program
‘ Author Guy Thomas https://computerperformance.co.uk/
‘ Version 2.7 – December 2010
‘ ———————— ——————————-‘
Option Explicit
Dim objWMIService, objProcess, colProcess
Dim strComputer, strProcessKill
strComputer = "."
strProcessKill = "’calc.exe’"

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

Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = " & strProcessKill )
For Each objProcess in colProcess
objProcess.Terminate()
Next
WSCript.Echo "Just killed process " & strProcessKill _
& " on " & strComputer
WScript.Quit
‘ End of WMI Example of a Kill Process

WMI Stop Process Tutorial – Learning Points

From a WMI Perspective

1)  This script builds on the basic WMI command in Example 1.  The heart of the script is the Win32_Process. Once we have selected the strProcessKill, then we call for the .Terminate method to close the program without issuing any warning to the user.

From a VBScript Perspective

2) Study the VBScript syntax used just before the variable strProcessKill:
("Select * from Win32_Process Where Name = " & strProcessKill).  For example, see where the speech marks end in relation to the bracket.

3) Although the script only terminates one process, it still has to loop through all the running processes to select the process = strProcessKill.  For Each… In… Next handles this scripting structure.

Guy Recommends: WMI Monitor and It’s Free!Solarwinds Free WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your scripts.

Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory or Exchange Server. Give this WMI monitor a try – it’s free.

Download your free copy of WMI Monitor

Example 2 – WMI Script to Terminate a Process on a Distant Machine

This script builds on Example 1 and adds the ability to terminate a process on a remote machine.

Prerequisites for your WMI Script

Naturally, if the named process does not exist, there is nothing for the script to terminate.  Therefore, you need to start the process referenced on line 9, in my example this process (program) is calc.exe.

Note the .terminate method does not work with NT 4.0 or Windows 9x machines.

Instructions for Terminating a Process

  1. Copy and paste the example script below into notepad or a VBScript editor.
  2. Save the file with a .vbs extension, for example: ProcessKill.vbs 
  3. Double click ProcessKill.vbs and check processes in Task Manger, there should be no calc.exe.

‘ ProcessKillRemote.vbs
‘ Sample VBScript kill process
‘ Author Guy Thomas https://computerperformance.co.uk/
‘ Version 2.9 – December 2010
‘ ———————— ——————————-‘
Option Explicit
Dim objWMIService, objProcess, colProcess
Dim strComputer, strProcessKill, strInput
strProcessKill = "’calc.exe’"

‘ Input Box to get name of machine to run the process
Do
    strComputer = (InputBox(" ComputerName to Run Script",_
    "Computer Name"))
    If strComputer <> "" Then
    strInput = True
    End if
Loop until strInput = True

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

Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = " & strProcessKill )
For Each objProcess in colProcess
    objProcess.Terminate()
Next
WSCript.Echo "Just killed process " & strProcessKill _
& " on " & strComputer
WScript.Quit
‘ End of WMI Example of a Kill Process
 

Solarwinds Config GeneratorGuy Recommends: The Free Config Generator

SolarWinds’ Config Generator is a free tool, which puts you in charge of controlling changes to network routers and other SNMP devices.  Boost your network performance by activating network device features you’ve already paid for.

Guy says that for newbies the biggest benefit of this free tool is that it will provide the impetus for you to learn more about configuring the SNMP service with its ‘Traps’ and ‘Communities’. Try Config Generator now – it’s free!

Download your free copy of Config Generator

WMI Tutorial – VBScript Stop Process

From a WMI perspective

1)  This script builds on the basic WMI command in Example 1.  The heart of the script is the Win32_Process. Once we have selected the strProcessKill, then we call for the .Terminate method to close the program without issuing any warning to the user.

From a VBScript perspective

2) Study the VBScript syntax used just before the variable strProcessKill:
("Select * from Win32_Process Where Name = " & strProcessKill).  For example, see where the speech marks end in relation to the bracket.

3) Although the script only terminates one process, it still has to loop through all the running processes to select the process = strProcessKill.  For Each… In… Next handles this scripting structure.

Example PowerShell – Get Stop-Process

All I need is to pipe ‘Get-Process notepad’ into Stop-Process  The result is its output becomes the input for Stop-Process.  Here is the working example:

# PowerShell Kill Process
Clear-Host
Get-Process notepad | Stop-Process

Learning Points

Note 2:  It’s all down to the (|) pipe.

Note 3:  The knack is to employ Get-Process to get a grip on the named process object, then we use Stop-Process to remove it from the list of running programs.

Summary of VBScript Kill Process

This page builds logically and is based on the assumption you have a Windows Server or client machine.  We start with a simple WMI script which lists the processes (programs) then echoes the result to the screen.  The second example introduces the powerful .terminate method which kills the named program without warning the users.

See more VBScript process examples:

VBScript List Process   • VBscript Start Process   VBScript Stop Process

WMI Home   • VBScript Services   • PowerShell Processes   • PowerShell Stop Process

WMI Home   • WMI Win32   • VBScript Echo   • WMI VBScript   •Downlad Free WMI Monitor