Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get CPU usage for each core using the windows command line

Is it possible to print the current CPU usage for each core in the system?

This is what I have so far using powershell:

Get-WmiObject -Query "select Name, PercentProcessorTime from Win32_PerfFormattedData_PerfOS_Processor"
like image 291
Fidel Avatar asked Oct 16 '25 12:10

Fidel


2 Answers

It can be be done using the following powershell command:

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

Also you could create a file called get_cpu_usage.ps1 with the contents:

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
}

Then run it using:

powershell -executionpolicy bypass "get_cpu_usage.ps1"
like image 80
Fidel Avatar answered Oct 19 '25 10:10

Fidel


As an alternative, you can use Get-Counter command.

For example:

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

From my testing it's about 4 times faster (atleast on my machine) than querying WMI.

EDIT: After testing some more, repeated uses of the query are faster (got mean of 284 ms) because Get-Counter needs minimum of 1 second to get the samples.

like image 40
Syphirint Avatar answered Oct 19 '25 10:10

Syphirint



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!