08-20-2011, 02:36 PM
Here's another code I wrote lately in PowerShell scripting that will sort out a list of processes, and take that list sorting each object by CPU consumption in reverse order. From that sorted list it grabs the first 5 Processes in the list using a loop and gets the values of each object ProcessID and ProcessName. I've put the number of Top Processes to display in a variable ($Total) so that it's easy for you to choose how many you want to display with this script.
Preview:
Code:
# Created by Ace - Copyright 2011 TechLifeForum.net
$Proc = Get-Process | Sort-Object CPU -Descending
$Total = 5
$num = 1
write-host
foreach ($objItm in $Proc) {
If ($num -gt $Total) {
break #break the loop
}
write-host "$num) " -foregroundcolor "white" -nonewline; write-host $objItm.ProcessName -foregroundcolor "cyan" -nonewline; write-host " - CPU:"$objItm.CPU
write-host "# ID:" $objItm.ID -foregroundcolor "white"
write-host
$num += 1
}
write-host
write-host ------------------------------------------------------------------------ -foregroundcolor "cyan"
write-host These are your top $Total CPU consuming processes... -foregroundcolor "cyan"
write-host ------------------------------------------------------------------------ -foregroundcolor "cyan"
write-host
Preview: