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}"