WMI query - CPU LoadPercentage

13,687

Personally I'd go for the Win32_PerfRawData_PerfOS_Processor class because it is much more precise. You will need to query both PercentProcessorTime and TimeStamp_Sys100NS. Here you can find the exact formula.

Share:
13,687
LaPhi
Author by

LaPhi

Hello my name is Lars, I´m a Computer Science student and I work in the IT industry. My focus is PowerShell, SharePoint, Office 365, SQL, Security.

Updated on June 04, 2022

Comments

  • LaPhi
    LaPhi almost 2 years

    I´m searching for a better way to get the CPU load in percent with WMI from multiple systems(means different CPUs etc.). My code is working, but I think there is a better way to get over all CPU usage in percent.

    Any ideas?

    Thank you in advance!

    SelectQuery queryCpuUsage = new SelectQuery("SELECT * FROM Win32_Processor");
    ManagementObjectSearcher cpuUsage = new ManagementObjectSearcher(scope, queryCpuUsage);                                       
    ManagementObjectCollection cpuUsageCollection = cpuUsage.Get();                                                       
    
    foreach (ManagementObject queryObj in cpuUsageCollection)
    {                                           
    iCPU++;
    calcCPU = Convert.ToInt32(queryObj["LoadPercentage"]);
    perCPU = perCPU + calcCPU;
    }
    
    perCPU = perCPU / iCPU;
    
    cpuUsageCollection.Dispose();
    
    Console.WriteLine("LoadPercentage CPU: {0}", perCPU);