help with this script

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


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.

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

Get-process will do that.

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

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