Home ยป PowerShell ยป How to Get CPU Usage in PowerShell

How to Get CPU Usage in PowerShell

There are two different ways in PowerShell to get CPU usage in percentages.

  1. Using the Get-Counter command โ€“ to retrieve the CPU utilization percentage.
  2. Using the Get-WmiObject command โ€“ to retrieve the CPU load percentage.

In this article, we will discuss how to get CPU utilization in percentage using PowerShell commands.

How to Get CPU Utilization Percentage using Get-Counter

To get CPU utilization percentage using the Get-Counter command, follow the below steps.

  1. Open PowerShell Terminal
  2. Type the command (Get-Counter '\Processor(_Total)\% Processor Time').CounterSamples.CookedValue
  3. Hit Enter
  4. It will display the CPU utilization percentage.
(Get-Counter '\Processor(_Total)\% Processor Time').CounterSamples.CookedValue

In the above PowerShell script, the Get-Counter command retrieves the current value of the % Processor Time counter for the total processor usage (_Total) and returns it as the CPU Utilization percentage.

The _Total qualifier refers to all processors on the computer.

The %Processor Time counter measures the percentage of time that a processor is busy executing the instructions.

The output of the above PowerShell script to get CPU usage in percentage is given below.

PowerShell - Get CPU Utilization in Percentage
PowerShell โ€“ Get CPU Utilization in Percentage

How to Get CPU Load as Percentage in PowerShell

You can use the `Get-WmiObject` cmdlet in PowerShell to retrieve the CPU information, including the current CPU load as a percentage.

(Get-WmiObject -Class Win32_Processor | Measure-Object -Property LoadPercentage -Average).Average

In the above PowerShell script, the Get-WmiObject cmdlet uses the Win32_Processor class. The Win32_Processor class represents a processor on a computer.

The LoadPercentage property of the Win32_Processor class represents the percentage of the time that the processor is busy executing the instructions.

The .Average property of the Measure-Object object returns the average value of the property. In this case, it will return the average CPU utilization across all processor cores.

The output of the above PowerShell command to get CPU usage is given below.

PS C:\> (Get-WmiObject -Class Win32_Processor | Measure-Object -Property LoadPercentage -Average).Average                                         

13
PS C:\>  

Cool Tip: How to get CPU usage by process in PowerShell!

Conclusion

I hope the above article on how to get CPU usage in percentage using the PowerShell commands Get-Counter and Get-WmiObject is helpful to you.

The Get-WmiObject command that uses the Win32_Processor class provides a more detailed view of CPU usage in case you have a multi-core CPU.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.