John Doe
John Doe

Reputation: 13

Listed highest CPU usage windows

I can get list the processes but how would I get them to show by highest usage instead of alphabetically?

Wmic path win32_performatteddata_perfproc_process get Name,PercentProcessorTime

Upvotes: 1

Views: 170

Answers (1)

Santiago Squarzon
Santiago Squarzon

Reputation: 59780

From you don't need to make direct calls to wmic, Get-CimInstance is meant to easily query all instances of WMI and CIM classes and output objects which are easy to manipulate. Sorting objects in PowerShell can be done with Sort-Object.

Get-CimInstance Win32_PerfFormattedData_PerfProc_Process |
    Sort-Object PercentPrivilegedTime -Descending |
    Select-Object Name, PercentProcessorTime

You could even go one step further and group the objects by their name with the help of Group-Object:

Get-CimInstance Win32_PerfFormattedData_PerfProc_Process |
    Group-Object { $_.Name -replace '#\d+$' } | ForEach-Object {
        [pscustomobject]@{
            Instances = $_.Count
            Name      = $_.Name
            PercentProcessorTime = [Linq.Enumerable]::Sum([int[]] $_.Group.PercentProcessorTime)
        }
    } | Sort-Object PercentProcessorTime -Descending

Upvotes: 1

Related Questions