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
CodePudding user response:
From powershell 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