使用 Windows 命令行获取每个内核的 CPU 使用率



是否可以打印系统中每个内核的当前CPU使用率?

这是我到目前为止使用powershell的:

Get-WmiObject -Query "select Name, PercentProcessorTime from Win32_PerfFormattedData_PerfOS_Processor"

可以使用以下powershell命令来完成:

(Get-WmiObject -Query "select Name, PercentProcessorTime from Win32_PerfFormattedData_PerfOS_Processor") | foreach-object { write-host "$($_.Name): $($_.PercentProcessorTime)" };

您还可以创建一个名为 get_cpu_usage.ps1 的文件,其中包含以下内容:

while ($true)
{
    $cores = (Get-WmiObject -Query "select Name, PercentProcessorTime from Win32_PerfFormattedData_PerfOS_Processor")
    $cores | foreach-object { write-host "$($_.Name): $($_.PercentProcessorTime)" }; 
    Start-Sleep -m 200
}

然后使用以下方法运行它:

powershell -executionpolicy bypass "get_cpu_usage.ps1"

作为替代方法,您可以使用Get-Counter命令。

例如:

Get-Counter -Counter 'Processor(*)% Processor Time' -Computer $desktop | select -ExpandProperty CounterSamples

根据我的测试,它比查询 WMI 快约 4 倍(至少在我的机器上)。

编辑:经过更多的测试,重复使用查询更快(得到284 ms的平均值),因为Get-Counter至少需要 1 秒才能获得样本。

在Powershell Core 6中,命令已更改。

(Get-CimInstance -Query "select Name, PercentProcessorTime from Win32_PerfFormattedData_PerfOS_Processor") | foreach-object { write-host "$($_.Name): $($_.PercentProcessorTime)" };

该脚本在Powershell Core 6中如下所示。

while ($true) {
         $cores = (Get-CimInstance -Query "select Name, PercentProcessorTime from Win32_PerfFormattedData_PerfOS_Processor")
         $cores | foreach-object { write-host "$($_.Name): $($_.PercentProcessorTime)" };
         Start-Sleep -m 1000
         [System.Console]::Clear() 
}

我只是喜欢更新之间的屏幕清除。 :)

最新更新