title: How Do You Monitor The CPU Utilization of a Process Using PowerShell?
body: <p>I'm trying to write a PowerShell script to monitor % CPU utilization of a SQL Server process. I'd like to record snapshots of this number every day so we can monitor it over time and watch for trends.</p>
  
  <p>My research online said this WMI query should give me what I want:</p>
  
  <pre><code>Get-WmiObject -Query "SELECT PercentProcessorTime FROM win32_PerfFormattedData_PerfProc_Process WHERE Name='SqlServr'"
  </code></pre>
  
  <p>When I run the WMI query I usually get a value somewhere between 30-50%
  <img src="https://i.stack.imgur.com/heVqY.png" alt="enter image description here"></p>
  
  <p>However, when I watch the process in Resource Monitor it usually averages at less than 1% CPU usage
  <img src="https://i.stack.imgur.com/w852L.png" alt="SQL Server Average CPU Utilization"></p>
  
  <p>I know the WMI query is simply returning snapshot of CPU usage rather than an average over a long period of time so I know the two aren't directly comparable. Even so, I think the snapshot should usually be less than 1% since the Resource Monitor average is less than 1%.</p>
  
  <p>Does anybody have any ideas on why there is such a large discrepancy? And how I can get an accurate measurement of the CPU usage for the process?</p>