0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script to Check CPU Usage Easily

Use Get-Counter '\Processor(_Total)\% Processor Time' in PowerShell to check current CPU usage percentage.
📋

Examples

InputRun script on idle system
OutputCPU Usage: 3.45 %
InputRun script under load
OutputCPU Usage: 75.12 %
InputRun script on multi-core system
OutputCPU Usage: 22.89 %
🧠

How to Think About It

To check CPU usage, ask the system for the current processor time percentage using a built-in performance counter. This gives a snapshot of how busy the CPU is right now. The script fetches this value and shows it in a simple number format.
📐

Algorithm

1
Request the CPU usage counter from the system.
2
Extract the CPU usage value from the returned data.
3
Format the value as a percentage with two decimals.
4
Display the formatted CPU usage to the user.
💻

Code

powershell
try {
  $cpu = Get-Counter '\Processor(_Total)\% Processor Time'
  $usage = $cpu.CounterSamples.CookedValue
  Write-Output "CPU Usage: {0:N2} %" -f $usage
} catch {
  Write-Output "Failed to get CPU usage."
}
Output
CPU Usage: 12.34 %
🔍

Dry Run

Let's trace running the script on a system with 12.34% CPU usage.

1

Get CPU counter

Get-Counter returns an object with CPU usage data, e.g., 12.34

2

Extract usage value

Extracted value is 12.34 from the counter samples

3

Format and output

Formatted string 'CPU Usage: 12.34 %' is printed

StepActionValue
1Get-Counter '\Processor(_Total)\% Processor Time'12.34
2Extract CookedValue12.34
3Output formatted stringCPU Usage: 12.34 %
💡

Why This Works

Step 1: Get-Counter fetches CPU data

The Get-Counter cmdlet queries Windows performance counters, here specifically the total CPU usage.

Step 2: Extract the numeric value

The CPU usage percentage is inside CounterSamples.CookedValue, which is a number representing usage.

Step 3: Format output for readability

Using -f formats the number to two decimals and adds a percent sign for clear display.

🔄

Alternative Approaches

Using WMI
powershell
try {
  $cpuLoad = Get-WmiObject Win32_Processor | Measure-Object -Property LoadPercentage -Average | Select-Object -ExpandProperty Average
  Write-Output "CPU Usage: $cpuLoad %"
} catch {
  Write-Output "Failed to get CPU usage via WMI."
}
WMI method is simpler but may be slower and less real-time than Get-Counter.
Using PerformanceCounter .NET class
powershell
Add-Type -AssemblyName System.Diagnostics
$cpuCounter = New-Object System.Diagnostics.PerformanceCounter('Processor', '% Processor Time', '_Total')
Start-Sleep -Seconds 1
$usage = $cpuCounter.NextValue()
Write-Output "CPU Usage: {0:N2} %" -f $usage
This approach uses .NET classes directly and requires a short delay for accurate reading.

Complexity: O(1) time, O(1) space

Time Complexity

The script performs a single system query without loops, so it runs in constant time.

Space Complexity

It uses a fixed amount of memory to store the counter data and output string.

Which Approach is Fastest?

Using Get-Counter is fast and real-time, WMI is simpler but slower, and .NET class method requires a delay but offers flexibility.

ApproachTimeSpaceBest For
Get-CounterO(1)O(1)Real-time CPU usage with built-in cmdlet
WMIO(1)O(1)Simple average CPU load, less real-time
.NET PerformanceCounterO(1) + delayO(1)Custom .NET usage with more control
💡
Run the script in PowerShell with administrator rights for best accuracy.
⚠️
Beginners often forget to escape backslashes in the counter path string.