≡ Menu

Use PowerShell to monitor CPU ulitization

You can use this simple command to query CPU utilization of local/remote machine with PowerShell. This is really useful for troubleshooting issues. Enjoy…

Get-Counter -ComputerName "RemotePC" '\Processor(*)\% Processor Time' -Continuous -SampleInterval 5

This queries remote computer CPU usage for every 5 seconds continously.

If you want to query the local computer CPU utilization, simply use below command.

Get-Counter '\Processor(*)\% Processor Time' -Continuous -SampleInterval 5

Hope you liked it.

{ 13 comments… add one }
  • AKP May 29, 2011, 6:44 pm

    Hi Techibee,

    Thank you for your script. But
    I have a problem while implementing this script, let me know how to start:

    I want to get an alert if CPU usage is over 85% and stay for more then 2 min continuous then alert via email. Pl send me some power shell script for this issue.

    Thanks
    AKP

  • Sitaram Pamarthi May 31, 2011, 10:44 pm
  • Rob Seymour August 2, 2011, 7:10 pm

    Hello,

    This is a lovely little script, but how can I return the statistics for the individual processes that are consuming the CPU resources? It is great to know that the overall CPU usage is 95% but to know that SQL server is using 85% of that would be really helpful for troubleshooting.

    I know this is an old thread but can you help out here?

    Thanks

    Rob

  • gerby March 7, 2012, 11:51 pm

    no worky…

    • Sitaram Pamarthi March 12, 2012, 10:30 pm

      @gerby, I see that you commented on two posts in this blog saying the solution is “not working”. That is very generic statement and I request you to post the exact error message you are seeing. That helps me to comment better.

  • KrishnaV September 20, 2012, 8:03 am

    Could you please provide powershall command for finding list of users utilizing using high cpu

  • tory burch outlet store April 28, 2013, 3:00 am

    only the The massive question is, Who this the Mangshan ghosting door of the ancestors do not know, I’m afraid that 12 cents of gold in order to understand the inside story.
    Addition to the style world clock off, which is one belonging to the reasons why Turkey door debilitating. Later, these angel-class people plus from another sector supernatural power to go mountains turn Yue moved to the amount of ordinary people living while in the Kunlun community, this selection between your spiritual roots of individuals from all passed along these style!

  • Greg Bray April 2, 2014, 11:16 pm

    Looks like the slashes in the script were removed. It should be: ‘\Processor(*)\% Processor Time’ not ‘Processor(*)% Processor Time’

  • derek June 30, 2014, 7:54 pm

    You’re missing 2 backslash(s) in the path of the second command’s counter.

    Get-Counter ‘Processor(*)% Processor Time’ -Continuos -SampleInterval 5
    should be:
    Get-Counter ‘\Processor(*)\% Processor Time’ -Continuos -SampleInterval 5

  • tim November 20, 2014, 11:29 am

    $i = 1
    $folder = Read-Host “What would you like to call the folder that you would like to save the Top Proccesses in?”
    $docname = Read-Host “What would you like to name the text file?”

    if ( -Not (Test-Path “c:\$folder”))
    {
    New-Item -Path “c:\$Folder” -ItemType directory | out-null
    }

    if ( -Not (Test-Path “c:\$folder\$docname”))
    {
    New-Item -Path “c:\$Folder\$docname.txt” -ItemType file -Force | out-null
    }

    $CPUPercent = @{
    Name = ‘CPUPercent’
    Expression = {
    $TotalSec = (New-TimeSpan -Start $_.StartTime).TotalSeconds
    [Math]::Round( ($_.CPU * 100 / $TotalSec), 2)
    }
    }

    do{

    Get-Process |
    Select-Object -Property Name, CPU, $CPUPercent, Description |
    Sort-Object -Property CPUPercent -Descending |
    where-object {$_.CPUPercent -gt 5} |
    out-file -filepath c:\$Folder\$docname.txt -encoding ASCII -width 70 -Append

    $i++
    Start-Sleep -s 10

    } while ($i -lt 6)

    cls

    [System.Media.SystemSounds]::Beep.Play()
    Write-Host
    Write-Host
    Write-Host
    Write-Host
    Write-Host
    Write-Host
    Write-Host
    Write-Host
    Write-Host ———————————————————————— -foregroundcolor “cyan”
    Write-Host “Now wasn’t that fun.. Let’s See Whats in the file. :)” -foregroundcolor “cyan”
    write-host ———————————————————————— -foregroundcolor “cyan”

    pause

    Invoke-Item c:\$folder\$docname.txt

  • Arild Marthinsen November 24, 2014, 8:42 pm

    Works like a charm :).
    I added an to the end just to get it logged as well!
    Easy piece!

Leave a Comment