Powershell get-process cpu usage percentage

The PowerShell command “Get-Process” is used to retrieve information about the processes running on a Windows computer.

To get the CPU usage percentage of processes, you can use the following command:

    
      $processes = Get-Process
      $cpuUsage = $processes | Select-Object -Property Name, CPU
      $totalCpuUsage = ($cpuUsage | Measure-Object -Property CPU -Sum).Sum

      foreach ($process in $cpuUsage) {
          $name = $process.Name
          $cpuPercentage = ($process.CPU / $totalCpuUsage) * 100

          Write-Output "Process: $name, CPU Usage: $cpuPercentage%"
      }
    
  

Let’s break down the command step-by-step:

  • First, we use the “Get-Process” command to retrieve information about all running processes on the computer. This information is stored in the $processes variable.

  • Next, we select the “Name” and “CPU” properties from the $processes variable using the “Select-Object” command and assign the result to the $cpuUsage variable. This gives us a list of processes along with their CPU usage values.

  • We calculate the total CPU usage by summing up the “CPU” values of all processes using the “Measure-Object” command and storing it in the $totalCpuUsage variable.

  • Finally, we iterate over each process in the $cpuUsage variable and calculate its CPU usage percentage by dividing its “CPU” value by the total CPU usage and multiplying by 100. We then output the process name and its CPU usage percentage using the “Write-Output” command.

Here’s an example output of the command:

    
      Process: chrome.exe, CPU Usage: 5.4%
      Process: svchost.exe, CPU Usage: 12.1%
      Process: explorer.exe, CPU Usage: 1.9%
      Process: powershell.exe, CPU Usage: 0.6%
      Process: cmd.exe, CPU Usage: 0.3%
    
  

Leave a comment