← script library

Check CPU Usage with PowerShell

Checks & DiagnosticsAugust 17, 2026

Takes five processor readings five seconds apart and reports the average instead of a single instantaneous number, which on a busy server is almost always misleading. Anything above 85% raises an error so the run is marked as failed and the alert reaches whoever is on call.

powershell
function Write-Log {
    param($Message)
    Write-Host "<WRITE-LOG = `"*$Message*`">"
}

try {
    $cpuValues = @()

    # Collect CPU usage 5 times, 5 seconds apart
    1..5 | ForEach-Object {
        $cpuSample = Get-Counter '\Processor(_Total)\% Processor Time'
        $cpuValue = $cpuSample.CounterSamples[0].CookedValue
        $cpuValues += $cpuValue
        Start-Sleep -Seconds 5
    }

    # Calculate and output the average
    $avgCpu = [math]::Round(($cpuValues | Measure-Object -Average).Average, 2)
    Write-Log "CPU Average Usage: $avgCpu %"

    # Alert if average CPU exceeds 85%
    if ($avgCpu -gt 85) {
        Write-Error "CPU usage too high: $avgCpu %"
    }

} catch {
    Write-Log "Unable to gather CPU Information: Device not supported."
}

Run it across your fleet

This script runs as-is on a single host. Paste it into ServerEngine to schedule it, run it on a whole server group in parallel, and keep the credentials out of the file — see the scripts documentation and the credential store.

Ready when you are.

Try ServerEngine free for 7 days.