shell 列出的最高CPU使用率窗口

8yoxcaq7  于 2022-12-13  发布在  Shell
关注(0)|答案(1)|浏览(125)

我可以列出进程,但如何让它们按最高使用率而不是按字母顺序显示呢?
Wmic path win32_performatteddata_perfproc_process get Name,PercentProcessorTime

siv3szwd

siv3szwd1#

powershell中,您不需要直接调用wmicGet-CimInstance旨在轻松查询WMICIM类的所有示例,以及输出对象,这些都很容易操作。可以使用Sort-Object对PowerShell中的对象进行排序。

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

您甚至可以更进一步,在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

相关问题