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.
Comments on this entry are closed.
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
AKP, http://techibee.com/powershell/get-cpu-utilization-of-a-computer-using-powershell/996 answers your question.
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
no worky…
@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.
Could you please provide powershall command for finding list of users utilizing using high cpu
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!
Looks like the slashes in the script were removed. It should be: ‘\Processor(*)\% Processor Time’ not ‘Processor(*)% Processor Time’
Thanks for Highlighting that Greg. I corrected the formatting.
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
Thats right.Somehow they were missing. Correct it now.
$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
Works like a charm :).
I added an to the end just to get it logged as well!
Easy piece!
Please update the title “PowerShell to monitor CPU ulitization” — Utilization