从 Windows 命令提示符获取 CPU 使用率
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9097067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get CPU Usage from Windows Command Prompt
提问by Mike
How would I get total CPU Usage from Windows Command Prompt?:
我如何从 Windows 命令提示符获得总 CPU 使用率?:
Expected Output:
预期输出:
27%
回答by Alex K.
C:\> wmic cpu get loadpercentage
LoadPercentage
0
Or
或者
C:\> @for /f "skip=1" %p in ('wmic cpu get loadpercentage') do @echo %p%
4%
回答by mdm
The following works correctly on Windows 7 Ultimate from an elevated command prompt:
以下从提升的命令提示符在 Windows 7 Ultimate 上正常工作:
C:\Windows\system32>typeperf "\Processor(_Total)\% Processor Time"
"(PDH-CSV 4.0)","\vm\Processor(_Total)\% Processor Time"
"02/01/2012 14:10:59.361","0.648721"
"02/01/2012 14:11:00.362","2.986384"
"02/01/2012 14:11:01.364","0.000000"
"02/01/2012 14:11:02.366","0.000000"
"02/01/2012 14:11:03.367","1.038332"
The command completed successfully.
C:\Windows\system32>
Or for a snapshot:
或者对于快照:
C:\Windows\system32>wmic cpu get loadpercentage
LoadPercentage
8
回答by PowerApp101
typeperf "\processor(_total)\% processor time"
does work on Win7, you just need to extract the percent value yourself from the last quoted string.
确实适用于 Win7,您只需要自己从最后一个带引号的字符串中提取百分比值。
回答by Amit Naidu
typeperf
gives me issues when it randomly doesn't work on some computers (Error: No valid counters.
) or if the account has insufficient rights. Otherwise, here is a way to extract just the value from its output. It still needs rounding though:
typeperf
当它在某些计算机 ( Error: No valid counters.
)上随机不起作用或帐户权限不足时,会给我带来问题。否则,这是一种仅从其输出中提取值的方法。不过它仍然需要舍入:
@for /f "delims=, tokens=2" %p in ('typeperf "\Processor(_Total)\% Processor Time" -sc 3 ^| find ":"') do @echo %~p%
Powershell has two cmdlets to get the percent utilization for all CPUs: Get-Counter
(preferred) or Get-WmiObject
:
Powershell 有两个 cmdlet 可以获取所有 CPU 的利用率百分比:(Get-Counter
首选)或Get-WmiObject
:
Powershell "Get-Counter '\Processor(*)\% Processor Time' | Select -Expand Countersamples | Select InstanceName, CookedValue"
Or,
或者,
Powershell "Get-WmiObject Win32_PerfFormattedData_PerfOS_Processor | Select Name, PercentProcessorTime"
To get the overall CPU load with formatted output exactly like the question:
要获得与问题完全相同的格式化输出的整体 CPU 负载:
Powershell "[string][int](Get-Counter '\Processor(*)\% Processor Time').Countersamples[0].CookedValue + '%'"
Or,
或者,
Powershell "gwmi Win32_PerfFormattedData_PerfOS_Processor | Select -First 1 | %{'{0}%' -f $_.PercentProcessorTime}"
回答by Denis
For anyone that stumbles upon this page, none of the solutions here worked for me. I found this is the way to do it (in a batch file):
对于偶然发现此页面的任何人,这里的任何解决方案都不适合我。我发现这是这样做的方法(在批处理文件中):
@for /f "skip=1" %%p in ('wmic cpu get loadpercentage /VALUE') do (
for /F "tokens=2 delims==" %%J in ("%%p") do echo %%J
)