none
Powershell command to get cpu percentage as displayed in task manager RRS feed

  • Question

  • Hi,

    I wish to extract the memory and cpu data of each process as shown in task manager. I am able to get the memory size in mb, but PercentProcessorTime does not return the exact process's cpu % value shown in task manager.

    function GetProcessInfoByName ([string]$processName)
    {
        Get-WmiObject -class Win32_PerfFormattedData_PerfProc_Process | where{$_.name -eq $processName} | Select-Object
        @{Name="Process Name"; Expression = {$_.name}},
        @{Name="CPU (%)"; Expression = {$_.PercentProcessorTime}},    
        @{Name="Memory (MB)"; Expression = {[Math]::Round(($_.workingSetPrivate / 1mb),2)}} 
    }
    
    Get-Process | ForEach-Object {$proc = $_; GetProcessInfoByName $proc.Name} | Format-Table -AutoSize

    Please help! Thanks.

    • Moved by Bill_Stewart Tuesday, June 16, 2015 7:42 PM Poor quality question/shows no research effort
    Thursday, April 16, 2015 2:08 AM

All replies

  • I think this is what you are trying to do. What you have posted is unworkable due to syntax issues.

    $properties=@(
        @{Name="Process Name"; Expression = {$_.name}},
        @{Name="CPU (%)"; Expression = {$_.PercentProcessorTime}},    
        @{Name="Memory (MB)"; Expression = {[Math]::Round(($_.workingSetPrivate / 1mb),2)}}
    )
    Get-WmiObject -class Win32_PerfFormattedData_PerfProc_Process | 
        Select-Object $properties |
        Format-Table -AutoSize
    

    This is also about 100 times faster than your version because it does not repeatedly return all processes and select only one by name.

    If you want only one process then use the filter and not the Where-Object.


    \_(ツ)_/

    Thursday, April 16, 2015 2:39 AM
  • Here is how to use the filter:

    $properties=@(
        @{Name="Process Name"; Expression = {$_.name}},
        @{Name="CPU (%)"; Expression = {$_.PercentProcessorTime}},    
        @{Name="Memory (MB)"; Expression = {[Math]::Round(($_.workingSetPrivate / 1mb),2)}}
    )
    
    $name='lsass'
    Get-WmiObject -class Win32_PerfFormattedData_PerfProc_Process -filter "Name='$name'" | 
        Select-Object $properties |
        Format-Table -AutoSize
    


    \_(ツ)_/

    Thursday, April 16, 2015 2:41 AM
  • It is actually not that simple, because of the varying values and this makes for some funny results this way:

    Thursday, April 16, 2015 8:02 AM
  • You'll find that determining the exact CPU usage is very difficult because of the always changing value and also because you want an average, not a peak at a particular point in time.

    As an example, on my task manager my powershell_ise was never consuming 12% CPU (not even 1%), that was a peak that got caught by the script (as seen in my screenshot above).

    I found I get more consistent results (not perfect though) with this variation of the script, which uses Get-Counter to query information:

    $CpuCores = (Get-WMIObject Win32_ComputerSystem).NumberOfLogicalProcessors
    (Get-Counter "\Process(*)\% Processor Time").CounterSamples | Select InstanceName, @{Name="CPU %";Expression={[Decimal]::Round(($_.CookedValue / $CpuCores), 2)}}
    

    The only problem here is that this does not give you the memory, which is something you're also after.

    And while you could join it to the results of the memory retrieved in another way, it doesn't return a PID, just a process name so if you have same many instances of the same program open... you'll get into trouble trying to match them.

    You could of course just join them all as one and say... I have 20 powershell.exe processes open and as a whole they're using 10% CPU and 200Mb ram, but that might not be what you're after...

    Thursday, April 16, 2015 9:05 AM
  • I found this:

    $sleepseconds = 1
    $numcores = 4
    $id = 872
    $cpu1 = (get-process -Id $id).cpu
    sleep $sleepseconds
    $cpu2 = (get-process -Id $id).cpu
    $cpupercent = [int](($cpu2 - $cpu1)/($numcores*$sleepseconds) * 100)

    Monday, October 1, 2018 2:24 PM
  • Hi, this is what i looking for.

    than you.

    i added these lines to the code 

    "| sort *CPU* -Descending | select -First 10"

    $CpuCores = (Get-WMIObject Win32_ComputerSystem).NumberOfLogicalProcessors
    (Get-Counter "\Process(*)\% Processor Time").CounterSamples | Select InstanceName, @{Name="CPU %";Expression={[Decimal]::Round(($_.CookedValue / $CpuCores), 2)}} | sort *CPU* -Descending | select -First 10"

    Best Regards, gavraham

    Monday, February 4, 2019 1:22 PM
  • I have the same problem.

    When I try to run the code on my Windows 10 PC I get an error saying (in Italian) something like: Get-counter: Impossible to find the specified object in the computer.

    ShouldI add any further instruction?

    Regards

    Mario

    PS C:\Users\user1> $CpuCores = (Get-WMIObject Win32_ComputerSystem).NumberOfLogicalProcessors
    (Get-Counter "\Process(*)\% Processor Time").CounterSamples | Select InstanceName, @{Name="CPU %";Expression={[Decimal]::Round(($_.CookedValue / $CpuCores), 2)}}
    Get-Counter : Impossibile trovare l'oggetto specificato nel computer.
    In riga:2 car:2
    + (Get-Counter "\Process(*)\% Processor Time").CounterSamples | Select  ...
    +  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidResult: (:) [Get-Counter], Exception
        + FullyQualifiedErrorId : CounterApiError,Microsoft.PowerShell.Commands.GetCounterCommand

    Tuesday, February 25, 2020 3:54 PM
  • > "When I try to run the code on my Windows 10 PC I get an error saying (in Italian) something like: Get-counter: Impossible to find the specified object in the computer."

    Get-Counter performance counter names are localized, so they are differently named, depending on the language your computer uses. Credit to this clever chap Tobias Weltner (https://www.powershellmagazine.com/2013/07/19/querying-performance-counters-from-powershell/).

    Monday, May 18, 2020 7:14 PM
  • Hello Mario, 

    Late reply but in case you're still wondering about this, performance counters' names depend on OS language 

    The above examples will work on any english system, but will not on an italian system, you need to translate the counters' names 

    To find out how it's named in Italian, just type get-counter with no additional parameter, and the italian version of "\Process(*)\% Processor Time" will be in the result

    Sunday, September 20, 2020 10:33 AM