Shell – windows powershell displaying CPU percentage and memory usage

powershell

I am able to use the answer CPU percentage.

While(1) { $p = get-counter '\Process(*)\% Processor Time'; cls; 
$p.CounterSamples | sort -des CookedValue | select -f 15 | ft -a}

Now I'm having problems with being able to have memory usage and CPU percentage displayed at the same time for a task on the computer.
I know Get-Counter "\Process(*)\Working Set - Private" will get the memory but id like to have task, CPU % and Memory usage being displayed at the same time.

UPDATE 1: Ideally I'd like to be able to run this in powershell as a set of commands. I would like the output to be displayed as:

Process Name CPU (%) Memory (MB)
------------ ------- -----------

I will take what I can get when it come to display that can be handled after being able to just displaying the three items in powershell.

UPDATE 2: This section of code provides what I would like

while (1) {cls; Get-WmiObject Win32_PerfFormattedData_PerfProc_Process |
  select-object -property Name, PercentProcessorTime, IDProcess, @{"Name" = "WSP(MB)"; Expression = {[int]($_.WorkingSetPrivate/1mb)}} |
  ? {$_.Name -notmatch "^(idle|_total|system)$"} |
  Sort-Object -Property PercentProcessorTime -Descending|
  ft -AutoSize |
  Select-Object -First 15; start-sleep 5}

This results in the following display:

Name                   PercentProcessorTime IDProcess                   WSP(MB)
----                    -------------------- ---------                   -------
MicrosoftEdgeCP#3                          6     18972                        6 WmiPrvSE#3                                 6     27300                        7
svchost#22                                 0      7316                        1

Only down side of this is that it fails after one display of information and I don't know why. Any help to this?

UPDATE:3

$cores = (Get-WmiObject Win32_Processor).NumberOfLogicalProcessors
while ($true) {
    $tmp = Get-WmiObject Win32_PerfFormattedData_PerfProc_Process | 
    select-object -property Name, @{Name = "CPU"; Expression = {($_.PercentProcessorTime/$cores)}}, @{Name = "PID"; Expression = {$_.IDProcess}}, @{"Name" = "Memory(MB)"; Expression = {[int]($_.WorkingSetPrivate/1mb)}} |
    Where-Object {$_.Name -notmatch "^(idle|_total|system)$"} |
    Sort-Object -Property CPU -Descending|
    Select-Object -First 15;
    cls;
    $tmp | Format-Table -Autosize -Property Name, CPU, PID, "Memory(MB)";
    Start-Sleep 5
}

Accomplishes all that I am in need of and gives me a starting place in the future to build off of. The issue I had with the table formatter is that format-table must be last.

Best Answer

There is another way to query performance counters via WMI that I think will deliver what you are after. The output also includes the process id which is useful when tracking a process that has multiple instances. It also uses what is called a "calculated property" within select-object to convert the working set value from bytes to megabytes. Also note that the max CPU on a 4 core system is 400, not 100. So to get the overall utilization (that being a max of 100) you need to divide each processes' CPU value by the number of cores.

$cores = (Get-WmiObject Win32_Processor).NumberOfLogicalProcessors
while ($true) {
    Get-WmiObject Win32_PerfFormattedData_PerfProc_Process | 
    Where-Object {$_.Name -notmatch "^(idle|_total|system)$" -and $_.PercentProcessorTime -gt 0} |
    Format-Table -Autosize -Property @{Name = "CPU"; Expression = {[int]($_.PercentProcessorTime/$cores)}}, Name, @{Name = "PID"; Expression = {$_.IDProcess}}, @{"Name" = "WSP(MB)"; Expression = {[int]($_.WorkingSetPrivate/1mb)}}
    Start-Sleep 5
}

CPU Name            PID WSP(MB)
--- ----      --------- -------
  1 chrome        12476      64
  2 wuauclt        7504       0
  3 SearchIndexer 10128      22
  4 TIworker      11380     102
Related Question