Windows Batch File (WMI) - taskkill process based on memory usage

2

Is there a way using WMIC, bat files, or powershell, to kill a task with a particular task name if the amount of virtual memory the process is using is above a certain limit?

Specifically I want to kill any instance of the iexplore.exe process if its workingset (RAM usage) or Virtual memory allocation is above 1GB.

I can kill a task using taskkill using this cmd: taskkill.exe /F /IM iexplore.exe and I can filter a list of processes and their RAM usage using this cmd wmic process where "name='iexplore.exe' and workingsetsize>=1000000000" get name, workingsetsize, processid.

Darwyn

Posted 2014-08-04T02:19:10.797

Reputation: 141

Answers

2

Here's the command that I eventually used:

PowerShell -Command "get-process | ? {$_.WorkingSet64 -gt 10000000000 -AND ($_.name -like 'java' -OR $_.name -like 'iexplore')} | foreach {kill -id $_.Id -force}"

It uses powershell to kill all processes named either java or iexplore if they exceed 1GB of RAM usage.

Darwyn

Posted 2014-08-04T02:19:10.797

Reputation: 141