Skip to content

Instantly share code, notes, and snippets.

@emnavarro02
Created February 4, 2020 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emnavarro02/996f40a3982417c656e4142b0e1eb280 to your computer and use it in GitHub Desktop.
Save emnavarro02/996f40a3982417c656e4142b0e1eb280 to your computer and use it in GitHub Desktop.
Get top process CPU consumers
#######################################################################################################################
# Get Top process CPU Consumers
# Author: Emerson Navarro
# Version: 0.1
#######################################################################################################################
# CHANGE LOG:
# - 04/02/2019:
# - Initial PID Collumn
#######################################################################################################################
Param(
# The amount of processes returned by the function. Default is 10.
[Parameter(Mandatory=$false)]
[int] $TOP=10
)
$PERF_PROCESS_QUERY = "SELECT * FROM Win32_PerfFormattedData_PerfProc_Process WHERE NOT (Name like 'idle' OR Name like '_Total')"
Get-WmiObject -Query $PERF_PROCESS_QUERY | Sort-Object PercentProcessorTime -Descending |
Select-Object Name, @{N="ID";E={$_.IDProcess}}, @{N="CPU (%)";E={$_.PercentProcessorTime}}, @{N="Command";E={$(Get-WmiObject -Query "SELECT CommandLine from Win32_Process WHERE ProcessId= $($_.IDProcess)").CommandLine}} -First $TOP
@emnavarro02
Copy link
Author

This script has an optional parameter to control the amount of processes returned. If no value is provided, script returns top 10 processes

To use this script:

powershell.exe -ExecutionPolicy <_Your_Execution_Policy_> -file .\Get-CPUConsumptionPerProcess.ps1 -Top _<10>_

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment