user1834664
user1834664

Reputation: 451

How to calculate windows cpu usage

I am trying to find which process is consuming the most CPU. I am running the below command

Get-Process | Sort-Object CPU -desc | Select-Object -first 10

output:

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
446      15    15920      11456  29,413.36   1216   0 svchost
548      21    16932      19544   6,929.14   3048   0 svchost
360      21   100236      37108   6,720.61  13812   0 WmiPrvSE
617      36   177652      95728   5,051.56   3708   0 RepMgr
4298       0      196         20   3,123.17      4   0 System
230      18    63936      58656   2,621.58   1344   0 svchost
1139      79   283204     172980   1,956.97   2464   0 MsMpEng
380      13     5444       5808   1,399.78     80   0 svchost
1718      25     8484      13320     858.80    708   0 lsass
3002     112    88856     135276     680.59   6964   1 explorer

also, when I ran the below command

but when I see the task manager

topmost CPU utilization process is cmd.exe and wmiprvSE.exe

why does the command which I have used not display cmd.exe as the topmost CPU utilization process?

Upvotes: 0

Views: 880

Answers (2)

CFou
CFou

Reputation: 1190

As you can notice with

Get-Process | Get-Member

...
CPU           ScriptProperty System.Object CPU {get=$this.TotalProcessorTime.TotalSeconds;} 

This means that CPU reflects only processes time (seconds spent to 100% equivalent CPU), and not CPU real time usage.

To get CPU real time usage :

Get-Counter -Counter "\Process(*)\% Processor Time" | Select -ExpandProperty CounterSamples | 
   Select InstanceName, RawValue | Sort RawValue -Descending | Select -First 10 -Skip 1

-Skip 1 is to hide the overall CPU usage.

Be aware that counters are localized. So, if you not use an english version of Windows, you need to change the counter name, for example, the french version is "\Processus(*)\% Temps processeur", sorry, I am afraid that I cannot translate much more :)

Upvotes: 1

imthor
imthor

Reputation: 553

It is also possible to have multiple cmd running processes on your machine and task manager could be summing it up and showing you the cumulative value. Can you try grouping it using ProcessName and then printing the top 10?

PS C:\> Get-Process | ? {$_.ProcessName -match "cmd"}

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName                                                                                                                         
-------  ------    -----      -----     ------     --  -- -----------                                                                                                                         
     81       7     3888       4940       0.02   9200   1 cmd                                                                                                                                 
     85       7     3072       5056       0.13  14496   1 cmd                                                                                                                                 

PS C:\> 
PS C:\> Get-Process | Group-Object -Property ProcessName | ForEach-Object {
            New-Object psobject -Property @{
                Item = $_.Name
                CPU = ($_.Group | Measure-Object CPU -Sum).sum
            }
        } | Sort-Object -Descending -Property CPU | Select-Object -first 10

       CPU Item               
       --- ----               
6806.09375 chrome             
1292.71875 WhatsApp           
426.640625 explorer           
295.328125 KillerControlCenter
   150.625 SystemSettings     
 86.453125 AutoHotkey         
 64.671875 Code               
  62.59375 WindowsTerminal    
    51.125 WINWORD            
      47.5 SearchApp          



PS C:\> 

Upvotes: 0

Related Questions