High CPU usage can significantly impact the performance of your system, leading to slow response times and reduced efficiency. By using PowerShell, you can easily identify and troubleshoot processes that consume excessive CPU resources. In this tutorial, I will explain how to monitor and manage CPU usage using Windows PowerShell.
Understanding CPU Usage in Windows
CPU usage refers to the amount of processing power used by computer applications and processes. When CPU usage is high, it can slow down your system and affect its performance. Monitoring CPU usage helps you identify which processes consume the most resources, allowing you to take appropriate action.
Monitoring CPU Usage with Get-Process in PowerShell
The Get-Process cmdlet in PowerShell is used to retrieve information about running processes. To get a list of all processes and their CPU usage, you can use the following command:
Get-Process | Sort-Object CPU -DescendingThis command sorts the processes by their CPU usage in descending order, allowing you to identify the most resource-intensive processes quickly.
Here is the exact output in the screenshot below:

Check out Disable Windows Defender Using PowerShell
Using PowerShell Get-Counter for Detailed CPU Metrics
For more detailed CPU metrics, the Get-Counter cmdlet is invaluable. This cmdlet retrieves performance counter data directly from the system. To get the current CPU usage, use the following command:
Get-Counter '\Processor(_Total)\% Processor Time'This command returns the percentage of time the processor spends executing non-idle threads. For continuous monitoring, you can run the command in a loop:
while ($true) {
Get-Counter '\Processor(_Total)\% Processor Time'
Start-Sleep -Seconds 5
}This script will display the CPU usage every five seconds, providing real-time monitoring.
Automate CPU Usage Alerts using PowerShell
To automate alerts for high CPU usage, you can create a script that sends notifications when CPU usage exceeds a certain threshold. Here’s an example script that sends an email alert if CPU usage goes above 80%:
$threshold = 80
$smtpServer = "smtp.example.com"
$smtpFrom = "alert@example.com"
$smtpTo = "admin@example.com"
$subject = "High CPU Usage Alert"
$body = "The CPU usage has exceeded the threshold."
while ($true) {
$cpuUsage = (Get-Counter '\Processor(_Total)\% Processor Time').CounterSamples.CookedValue
if ($cpuUsage -gt $threshold) {
Send-MailMessage -SmtpServer $smtpServer -From $smtpFrom -To $smtpTo -Subject $subject -Body $body
}
Start-Sleep -Seconds 60
}This script continuously monitors the CPU usage and sends an email notification if it exceeds the specified threshold.
Check out Install Snipping Tool in Windows 11 Using PowerShell
Identifying CPU-Intensive Processes
To identify which processes are consuming the most CPU, you can combine Get-Process with Sort-Object:
Get-Process | Sort-Object CPU -Descending | Select-Object -First 10This command lists the top 10 processes by CPU usage, helping you pinpoint the culprits.
Managing High CPU Usage
Once you’ve identified the processes using excessive CPU, you can take action to manage them. For example, you can stop a process using the Stop-Process cmdlet:
Stop-Process -Name "notepad" -ForceReplace “notepad” with the name of the process you want to stop. Use this command with caution, as forcibly stopping processes can lead to data loss.
Creating Custom CPU Monitoring Scripts
You can combine multiple cmdlets into a custom script to create a more comprehensive CPU monitoring solution. Here’s an example script that logs CPU usage to a file and sends an alert if usage exceeds 90%:
$threshold = 90
$logFile = "C:\cpu_usage_log.txt"
$smtpServer = "smtp.example.com"
$smtpFrom = "alert@example.com"
$smtpTo = "admin@example.com"
$subject = "Critical CPU Usage Alert"
$body = "The CPU usage has exceeded the critical threshold."
while ($true) {
$cpuUsage = (Get-Counter '\Processor(_Total)\% Processor Time').CounterSamples.CookedValue
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
"$timestamp - CPU Usage: $cpuUsage%" | Out-File -Append -FilePath $logFile
if ($cpuUsage -gt $threshold) {
Send-MailMessage -SmtpServer $smtpServer -From $smtpFrom -To $smtpTo -Subject $subject -Body $body
}
Start-Sleep -Seconds 60
}This script logs CPU usage to a file with a timestamp and sends an email alert if usage exceeds 90%.
Check out Get an IP Address Using PowerShell in Windows
Advanced CPU Monitoring with Performance Counters
You can use performance counters to track specific processes for more advanced monitoring. Here’s an example of monitoring the CPU usage of a process named “chrome”:
$processName = "chrome"
$counterPath = "\Process($processName)\% Processor Time"
while ($true) {
$cpuUsage = (Get-Counter $counterPath).CounterSamples.CookedValue
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
"$timestamp - $processName CPU Usage: $cpuUsage%" | Out-File -Append -FilePath "C:\chrome_cpu_usage_log.txt"
if ($cpuUsage -gt $threshold) {
Send-MailMessage -SmtpServer $smtpServer -From $smtpFrom -To $smtpTo -Subject $subject -Body "The CPU usage of $processName has exceeded the threshold."
}
Start-Sleep -Seconds 60
}This script monitors the CPU usage of the “chrome” process and logs it to a file.
Conclusion
In this tutorial, I explained how to monitor and manage CPU usage with Windows PowerShell, which ensures your system runs smoothly. By using cmdlets like Get-Process and Get-Counter, you can gather detailed CPU metrics and automate alerts for high usage.
You may also like:
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.