VB script kill highest process

hi any one help woth a script that sees how many processare running for notepad.exe and kills the one wusing most processor

tricky bit is defining which is the highest i think
mhamerAsked:
Who is Participating?
 
jawa29Connect With a Mentor Commented:
Hi mhamer

This script will do what you are asking.

Line 12 and 16 can be removed as these just pop up to tell which Processes are running and which Process ID will be closed.

Jawa29
sProcToKill = Null

Set oWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set cProcesses = oWMIService.ExecQuery("Select * from Win32_Process WHERE Name = 'Notepad.exe'")

For Each oProcess in cProcesses
    sngProcessTime = (CSng(oProcess.KernelModeTime) + CSng(oProcess.UserModeTime)) / 10000000
    If sngProcessTime >= sProcHigh Then
    	sProcHigh = sngProcessTime
    	sProcToKill = oProcess.processid
    End If
    Wscript.Echo oProcess.name & vbTab & oProcess.processid & vbTab & sngProcessTime
Next

If Not IsNull(sProcToKill) Then
	WScript.Echo "Process To Kill - " & sProcToKill
	Set cProcessList = oWMIService.ExecQuery("Select * from Win32_Process Where ProcessID = " & sProcToKill)
	
	For Each oProcess in cProcessList
	    oProcess.Terminate()
	Next
End If

Open in new window

0
 
mhamerAuthor Commented:
wow thanks,
perfect
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.