Support Forums

Full Version: Get Top 5 CPU Consuming Processes {PowerShell}
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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.

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:
[Image: ieaeFxEA1.png]
Looks nice, I have a Rainmeter app that does this for me though (Can't remember the name or what skin it's from though >.> lol)

Quite useful if you want to know what's using what =p
Rainmeter is nice for a quick display, and this is very simple for a powershell script though. Powershell is far more sophisticated than Rainmeter no doubt, but I wasn't sure what else I could do with processes. I could kill all the top 5 processes if I wanted using this script I created, but you probably don't want to do that lol. I have another I created before this to kill and restart explorer.exe:

Code:
$proc = Get-Process

foreach ($objItm in $proc) {
    If ($objItm.ProcessName -eq "explorer") {
        kill $objItm.ID
    }
}

write-host
write-host Explorer has been successfully restarted! -foregroundcolor "Magenta"
write-host

One thing about powershell is that you can send in full command outputs as variables to decide what the next command will do with it, you can split functions, and join them too.
Yea, don't think you exactly want to kill the top 5 users lmao, Could cause some issues =p

But we can check the same from windows task manager by pressing CTRL+ALT+DELETE ? right Huh
(08-20-2011, 03:11 PM)【 ¶ÑÇƦ€Ƿ¦ßŁΞ 】 Wrote: [ -> ]But we can check the same from windows task manager by pressing CTRL+ALT+DELETE ? right Huh

I would say this is a more detailed thing, but that's my opinion.

I think a nice edit to this would be to put all the processes not just the top 5 with the amount their using and the location of the file... Would be good over all but also nice for an anti-malware addition to it =p
(08-20-2011, 03:14 PM)Kom Wrote: [ -> ]I would say this is a more detailed thing, but that's my opinion.

I think a nice edit to this would be to put all the processes not just the top 5 with the amount their using and the location of the file... Would be good over all but also nice for an anti-malware addition to it =p

Just remove this bit then:
Code:
If ($num -gt $Total) {
        break #break the loop
    }

It already has a built in function for it though:
[Image: ilFkRNok.png]
Yea but it doesn't show the location of the file. Would be nice if it did (I already knew to break the loop lol was just trying to explain it)
Hmm... That's not a bad idea, I could put together something like that. I could Pipeline each object instance out and use the System.Diagnostics object with powershell to get the paths.
Here's a quick demo testfile I put together which saves the output of a process list with filepath's to a text file in your C:\ drive called "Output_Proc_List.txt"

Code:
$Process = @{Expression={$_.Name};Label="Process Name";width=25}, `
@{Expression={$_.Path};Label="Filepath"}

$Proc = Get-Process | Format-Table $Process | Out-File -Filepath C:\Output_Proc_List.txt

Try this:
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 $objItm.Path -foregroundcolor "white"
    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

that will display the top 5 CPU consuming processes with their filepath, for all of them, just remove that loop breaking block of code I have in there.



EDIT: Updated version
Code:
# Created by Ace - Copyright 2011 TechLifeForum.net

$Proc = Get-Process | Sort-Object CPU -Descending
$num = 1

write-host
foreach ($objItm in $Proc) {
    write-host "$num) " -foregroundcolor "white" -nonewline; write-host $objItm.ProcessName -foregroundcolor "magenta" -nonewline;
    If (!($objItm.CPU -eq $null)) {
        write-host " - CPU:"$objItm.CPU -foregroundcolor "white"
        } else {
            write-host " - CPU: " -foregroundcolor "white" -nonewline; write-host "N/A" -foregroundcolor "darkgray"
        }
    If (!($objItm.Path -eq $null)) {
        write-host $objItm.Path -foregroundcolor "cyan"
    } else {
        write-host "** [No filepath to this process]" -foregroundcolor "darkgray"
    }
    write-host "# ID:" $objItm.ID -foregroundcolor "white"
    write-host
    $num += 1
}

write-host
write-host -------------------------------------------------- -foregroundcolor "cyan"
write-host ">" You have ($num - 1) currently running processes... -foregroundcolor "cyan"
write-host -------------------------------------------------- -foregroundcolor "cyan"
write-host

[Image: ibYCN7cMZ.png]

** Note: If you use this instead, it will sort each by process name alphabetically after the ordered process numbered list
Code:
$Proc = Get-Process | Sort-Object ProcessName
(08-20-2011, 03:14 PM)Kom Wrote: [ -> ]I would say this is a more detailed thing, but that's my opinion.

I think a nice edit to this would be to put all the processes not just the top 5 with the amount their using and the location of the file... Would be good over all but also nice for an anti-malware addition to it =p

It's definitely more detailed, and I like it.
Keep up the good work, buddy.Thumbsup
Pages: 1 2