help with this script

This topic contains 5 replies, has 3 voices, and was last updated by  Vern Anderson 4 years, 2 months ago.

  • Author
  • #14645

    Avi Grinberg

    i want a script to capture the current CPU usage for every process above 0 CPU usage.
    i create this script but i see that the script doesn't report a right CPU usage like task manager show because it doesn't calculated the processor threads so i show 99 CPU usage. i found this code that show the CPU count [System.Environment]::ProcessorCount
    my question is how do i insert the code into the cookedvalue results that it will reflect like task manager?

    Get-Counter '\Process(*)\% Processor Time' | Select-Object -ExpandProperty countersamples | Select-Object -Property instancename, cookedvalue| where {$_.instancename -ne "_Total" -and $_.instancename -ne "Idle" -and $_.cookedvalue -gt 0} | Sort-Object -Property cookedvalue -Descending | ft -AutoSize


  • #14650

    Don Jones

    Have you considered just running Get-Process? That shows CPU usage on a per-process basis.

    But there's no way to insert a value into a performance counter.

    Also, [System.Environment]::ProcessorCount shows the *number of processors in the machine*, not the CPU usage.

    So I'm confused about what you want.

  • #14651

    Avi Grinberg

    basically i simple want to get the current CPU usage like task manager show in the processes...

  • #14652

    Don Jones

    Get-process will do that.

  • #14653

    Avi Grinberg

    no get-process will not show the active running process like task manager show in regard to the CPU usage.

  • #14660

    Vern Anderson

    This is about as close as I have gotten to what I think you're looking for.

    (Get-Counter "\\$ENV:COMPUTERNAME\process(*)\% processor time").CounterSamples | Where-Object {$_.InstanceName -notlike '_total'} | Sort-Object CookedValue -Descending | Format-Table @{Name="Process";Expression={$_.InstanceName}},@{Name="Percent CPU";Expression={$_.CookedValue/[int]$Env:NUMBER_OF_PROCESSORS}; Format="F2"; Alignment="Left"} -AutoSize

You must be logged in to reply to this topic.