One of the best ways to monitor CPU utilization with PowerShell is by querying performance counters. We can also get this information using WMI. In this recipe, you'll learn a few techniques that can be used to monitor CPU utilization using the Get-Counter
and Get-WmiObject
cmdlets.
To get an idea of the current CPU utilization for a server, we can gather data for the
Processor(_Total)\% Processor Time
performance counter:Get-Counter "\Processor(_Total)\% Processor Time" -Continuous
This would continuously output the total utilization across each CPU, as shown:
In addition, we can use the
Win32_Processor
class and select theLoadPercentage
property to determine the utilization for each CPU:Get-WmiObject Win32_Processor | select LoadPercentage
Both Get-Counter
and Get-WmiObject
support the -ComputerName
parameter and can be run against remote machines.