Get CPU Usage from Windows Command Prompt [closed]
Asked Answered
G

5

58

How would I get total CPU Usage from Windows Command Prompt?:

Expected Output:

27%
Gigigigli answered 1/2, 2012 at 14:4 Comment(5)
related question: #2415585Geoffrey
@Geoffrey - Related, but does not answer my questionGigigigli
@mdm typeperf "\processor(_total)\% processor time" - But it does not work on win 7Gigigigli
@Mike: i know ... that's why I said "related", not "duplicate" :PGeoffrey
I was after the CPU utilization value as shown in the task manager but none of the answers provided here worked for me. I used the command here instead: https://mcmap.net/q/331621/-batch-quot-wmic-cpu-quot-issue-with-multi-core-systemNoeminoesis
D
106
C:\> wmic cpu get loadpercentage
LoadPercentage
0

Or

C:\> @for /f "skip=1" %p in ('wmic cpu get loadpercentage') do @echo %p%
4%
Dunc answered 1/2, 2012 at 14:11 Comment(7)
Kudos to you! I have a question for this. Do you have code for this getting the CPU Usage of a remote computer or a server?Tremaine
In a doman environment with appropriate rights; wmic /node:machinename cpu get loadpercentage for other scenarios you will have to google wmic remote machineDunc
@AlexK. adding /every:1 repeats this in a loop.Fitment
you can just use wmic cpu get loadpercentage /value to get the value directly instead of skipping a lineRecusant
Please note: if you get an error that wmic is not recognized, the WMIC.exe resides normally in the C:\windows\system32\wbem directory, so use C:\windows\system32\wbem\wmic.exe if need beBlame
/value didn't work for me, so with grep installed I used @for /f %p in ('wmic cpu get loadpercentage ^| grep -P \d+') do @echo %p% insteadOloroso
when I run: @for /f "skip=1" %p in ('wmic cpu get loadpercentage') do @echo %p% I get: "4% %". what can I do to just get "4"?Relationship
B
17

The following works correctly on Windows 7 Ultimate from an elevated command prompt:

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
Bridie answered 1/2, 2012 at 14:11 Comment(0)
P
8
typeperf "\processor(_total)\% processor time"

does work on Win7, you just need to extract the percent value yourself from the last quoted string.

Pitarys answered 1/2, 2012 at 14:13 Comment(0)
A
3

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:

@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 "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:

Powershell "[string][int](Get-Counter '\Processor(*)\% Processor Time').Countersamples[0].CookedValue + '%'"

Or,

 Powershell "gwmi Win32_PerfFormattedData_PerfOS_Processor | Select -First 1 | %{'{0}%' -f $_.PercentProcessorTime}"
Aeolic answered 19/4, 2018 at 5:24 Comment(0)
L
0

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
)
Lamarre answered 14/5, 2020 at 22:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.