title: Continuously monitors the CPU usage % of top X processes
body: <p>I want to be able to to output to a log file the top CPU consumers every 5 seconds. That way I will be able to see who uses the most of the cpu during my tests.</p>
  
  <p>I have found this answer very common:</p>
  
  <pre><code>$cpu = Get-Counter -ComputerName localhost "\Process(*)\% Processor Time" `
      | Select-Object -ExpandProperty countersamples `
      | where {$_.InstanceName -ne 'idle' } `
      | where {$_.InstanceName -ne '_total' }`
      | Select-Object -Property instancename, cookedvalue `
      | Sort-Object -Property cookedvalue -Descending `
      | Select-Object -First 5 `
      | ft @{L='Date';E={Get-Date}}, InstanceName, @{L='CPU';E={(($_.Cookedvalue/100)/$NumberOfLogicalProcessors).toString('P')}} -HideTableHeaders `
      | Format-Table -Auto | Out-String
  </code></pre>
  
  <p>I have 2 issues with it:</p>
  
  <ol>
  <li><p>Sometimes I get:</p>
  
  <blockquote>
    <p>Get-Counter : The data in one of the performance counter samples is not valid. View the Status property for each PerformanceCounterSample object to make sure it contains valid data.</p>
  </blockquote></li>
  <li><p>I would like to get the full process name, and not </p>
  
  <pre>java      25%
  idea64    0.8%
  ...</pre></li>
  </ol>