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

    Pertanyaan

  • 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.

    • Dipindahkan oleh Bill_Stewart Selasa, 16 Juni 2015 19.42 Poor quality question/shows no research effort
    Kamis, 16 April 2015 02.08

Semua Balasan

  • 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.


    \_(ツ)_/

    Kamis, 16 April 2015 02.39
  • 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
    


    \_(ツ)_/

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

    Kamis, 16 April 2015 08.02
  • 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...

    Kamis, 16 April 2015 09.05
  • 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)

    Senin, 01 Oktober 2018 14.24