locked
CPU Percentage RRS feed

  • Question

  • Hi there,

    I would like to create a powershell script that will tell me what processes/programs are using what percentage of CPU, like you can see in Task Manager.

    I've got this script:

    get-wmiobject -cl Win32_PerfFormattedData_PerfProc_Process | ? {$_.Name -notlike '*_Total*' } | sort-object PercentPRocessorTime -desc | ft -auto Name,idprocess,percentprocessortime

    But it will routinely give me values that are not very helpful, or just plain wrong.

    For example, when I run it now I get this:

    Name                        idprocess percentprocessortime
    ----                             --------- --------------------                

    OUTLOOK                          5656                  100
    Idle                                        0                   35
    System                                   4                    5
    chrome#9                         1124                    5
    dwm                                   268                    5
    WRSA#1                           1180                    5
    svchost#26                       2440                    0

    If Outlook truly is 100%, how can anything else be greater than 1?

    I'm wanting it to run on a server, and if the total CPU is greater than 98%, then I want a list of running processes to let me know what is happening.

    Is there a way to do this?

    Thanks in advance,

    Steve.


    Friday, June 16, 2017 4:23 AM

Answers

  • It is a simple snapshot over a predefined interval.  It is minimally useful.

    \_(ツ)_/

    Thursday, June 22, 2017 5:07 AM

All replies

  • Hi Steve,

    The calculations are not real time.

    You could try using get-counter, for instance:

    $cpus=(get-counter -Counter "\Processor(_Total)\% Processor Time" -SampleInterval 1 -MaxSamples 5 |
        select -ExpandProperty countersamples | select -ExpandProperty cookedvalue | Measure-Object -Average).average
    If ($cpus -ge 98)
    {
       query process
       tasklist /m 
    }Else
    {
        #something else
    }

    Besides, there're many pre-written scripts on link below:

    https://gallery.technet.microsoft.com/site/search?query=monitor%20cpu&f%5B0%5D.Value=monitor%20cpu&f%5B0%5D.Type=SearchText&ac=4

    Best regards,

    Andy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    • Proposed as answer by Chen VMVP Friday, June 16, 2017 3:16 PM
    Friday, June 16, 2017 6:15 AM
  • Hi Andy,

    Thanks for that, however if I run those two commands:

    query /process

    tasklist /m

    ... neither of those give individual process CPU percentage. 

    I had a look through the scripts on that page, and I don't see any that will do that for me either. I'm not terribly concerned if they are not real-time but it is disconcerting to see values that add to greater than 100%.

    Where does task manager get those percentage values from?

    Thanks,

    Steve.

    Monday, June 19, 2017 2:06 AM
  • Compute the value of  TotalProcessorTime /(Now - StarrTime) as a percentage.

    get-process iexplore | select Name,StartTime,TotalProcessorTime

    That is how TM does it


    \_(ツ)_/

    • Proposed as answer by Hello_2018 Monday, June 19, 2017 3:03 AM
    Monday, June 19, 2017 2:18 AM
  • Ok, I think I get it. Because the Task Manager view is essentially a view over time, an instantaneous view (which is what I'm kinda after) is meaningless, as you need several data points to get the average. Would that be a fair summary?
    Monday, June 19, 2017 2:49 AM
  • Hi,

    >>Where does task manager get those percentage values from?

    Resource Monitor.

    At the bottom of  Performance label, click "open resource monitor", you'll see more details.

    Best regards,

    Andy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    • Proposed as answer by Hello_2018 Friday, July 7, 2017 4:58 AM
    Monday, June 19, 2017 3:03 AM
  • Try this:

    Get-WmiObject win32_processor | Measure-Object -property LoadPercentage -Average | Select Average

    Also, get-counter is perfect:

    https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.diagnostics/get-counter

    Best regards,

    Andy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    • Edited by Hello_2018 Monday, June 19, 2017 3:07 AM
    Monday, June 19, 2017 3:06 AM
  • Hi Andy,

    Thanks for that. So how would I use this code:

    Get-WmiObject win32_processor | Measure-Object -property LoadPercentage -Average | Select Average

    ... to get the average of one process, like Chrome, or all processes?

    Thanks,

    Steve.

    Tuesday, June 20, 2017 9:10 PM
  • Hi Steve,

    sorry, my mistake.

    win32_process not win32_processor(this get CPU info)

    Using: tasklist /v to get required process cpu usage.For instance:

    tasklist /v >task.txt

    Best regards,

    Andy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    • Edited by Hello_2018 Wednesday, June 21, 2017 2:25 AM
    Wednesday, June 21, 2017 2:24 AM
  • Hi again Andy,

    The win32_process does not have the LoadPercentage property, so I'm still stuck trying to get the percentage of CPU consumed by a single process.

    Do you have any more ideas?

    Thanks again,

    Steve.

    Thursday, June 22, 2017 3:16 AM
  • Load percentage is an instantaneous number and cannot be calculated by normal means.  You would sample over time and then calculate load percentage over time.  Load%/n-Minutes


    \_(ツ)_/

    Thursday, June 22, 2017 3:19 AM
  • Hi jrv,

    Ok, thanks. That's what I've been starting to suspect.

    Since that is the case, what values am I getting when I run the command that I mentioned in my original question:

    get-wmiobject -cl Win32_PerfFormattedData_PerfProc_Process | ? {$_.Name -notlike '*_Total*' } | sort-object PercentPRocessorTime -desc | ft -auto Name,idprocess,percentprocessortime

    -Steve.

    Thursday, June 22, 2017 3:48 AM
  • Please understand that there is no such measurement as "LoadPercentage".  You need to review what performance and, in particular, "process" performance can measure."load" is not a valid term in performance engineering.


    \_(ツ)_/

    Thursday, June 22, 2017 4:46 AM
  • It is a simple snapshot over a predefined interval.  It is minimally useful.

    \_(ツ)_/

    Thursday, June 22, 2017 5:07 AM
  • I had this very issue, not able to get  certain process average CPU% usage...once every second.

    Ended up running a scheduled job, then retrieving it's results - this gave me good precise figures ...

    Start-Job { while($true) { ((Get-Counter "\Process("processName")\% Processor Time").CounterSamples.CookedValue) ; Start-Sleep 1 } } -Name getCPU
    
    ####################
    Then inside my code:
    
    $ReceiveJob = Receive-Job getCPU;
    if ($ReceiveJob) {$CPU = [math]::Round($ReceiveJob[-1] / $cpu_cores)}
    
    ##################
    (I was dividing the percentage by the number of existing cores ...


    levW


    • Edited by Lev770 Monday, July 17, 2017 11:32 AM
    Monday, July 17, 2017 11:31 AM