Author Posts

April 20, 2014 at 3:22 am

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


April 20, 2014 at 6:02 am

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.

April 20, 2014 at 7:23 am

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

April 20, 2014 at 7:37 am

Get-process will do that.

April 20, 2014 at 1:32 pm

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

April 21, 2014 at 3:57 am

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