Skip to content

Instantly share code, notes, and snippets.

@keejelo
Last active November 25, 2021 16:06
Show Gist options
  • Select an option

  • Save keejelo/476bf9cbe6e750cef561ebab9debb3fd to your computer and use it in GitHub Desktop.

Select an option

Save keejelo/476bf9cbe6e750cef561ebab9debb3fd to your computer and use it in GitHub Desktop.
Terminates a running process (with optional wait timer)
'--------------------------------------------------------------------------------
' Filename: kill.vbs
'--------------------------------------------------------------------------------
' KillProcess v3.2 - Terminates a running process (program)
' Author: Keejelo
' gist.github.com/keejelo/476bf9cbe6e750cef561ebab9debb3fd
'--------------------------------------------------------------------------------
' Usage: kill "param1" param2
' @ param1 = "processname", i.e. "notepad.exe"
' @ param2 = seconds to wait until process is terminated (optional)
' Example: kill "notepad.exe" 10
'--------------------------------------------------------------------------------
Option Explicit
Dim objWMIService, objProcess, colProcess, strComputer, strProcessKill
If WScript.Arguments.Count = 0 Then
WScript.Echo( "ERROR: missing parameter!" & vbCrlf & vbCrlf & _
"Usage: kill ""param1"" param2" & vbCrlf & vbCrlf & _
"param1 = ""processname"", i.e. ""notepad.exe""" & vbCrlf & vbCrlf & _
"param2 = seconds to wait until process is terminated (optional)" & vbCrlf & vbCrlf & _
"Example: kill ""notepad.exe"" 10" )
Else
If WScript.Arguments.Count = 2 Then
WScript.Sleep( WScript.Arguments.Item(1) * 1000 )
End If
strComputer = "."
strProcessKill = WScript.Arguments.Item(0)
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcess = objWMIService.ExecQuery("Select * from Win32_Process Where Name = " & "'" & strProcessKill & "'")
On Error Resume Next
For Each objProcess in colProcess
objProcess.Terminate()
Next
End If
WScript.Quit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment