Categories

Top processes by resource

How many times have you typed something like

Get-Process | sort cpu -Descending | select -f 5

to full back the processes taking most CPU resources – or similar with any other of the process properties.

After a while it gets tiring typing the same old stuff so its time for a function

function get-topprocess {            
 [CmdletBinding()]            
 param (            
  [parameter(ParameterSetName="CPU")]            
  [switch]$cpu,            
              
  [parameter(ParameterSetName="Handle")]            
  [switch]$handles,            
              
  [parameter(ParameterSetName="NonpagedSystemMemory")]            
  [switch]$npm,            
              
  [parameter(ParameterSetName="PagedMemory")]            
  [switch]$pm,            
              
  [parameter(ParameterSetName="VirtualMemory")]            
  [switch]$vm,            
              
  [parameter(ParameterSetName="WorkingSet")]            
  [switch]$ws,            
              
  [int]$top=5,            
  [string]$computer="."            
 )            
             
 switch ($psCmdlet.ParameterSetName) {            
 "CPU"  {$sort = "CPU" ; break}            
 "Handle"  {$sort = "HandleCount" ; break }            
 "NonpagedSystemMemory"  {$sort = "NonpagedSystemMemorySize" ; break }            
 "PagedMemory"  {$sort = "PagedMemorySize" ; break }            
 "VirtualMemory"  {$sort = "VirtualMemorySize" ; break }            
 "WorkingSet"  {$sort = "WorkingSet" ; break }            
 default {Write-Host "Error!!! Should not be here" }            
}            
            
Get-Process -ComputerName $computer |            
sort $sort -Descending |            
select -First $top            
            
}


Set up a bunch of switches to act as the sort property – use parameter sets to make them mutually exclusive. Other parameters take the number to return and the computer we are interested in.



Plug the variables into the pipeline and jobs a good un.



Examples of using



get-topprocess -cpu
get-topprocess -handles
get-topprocess -npm
get-topprocess -pm
get-topprocess -vm
get-topprocess -ws
get-topprocess -cpu -top 10

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>