1

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.

7
  • "Displayed at the same time"...at a command prompt? In a GUI? In Perfmon? please provide more details. What does your code look like? How are you displaying CPU usage? What would your end goal look like?
    – EBGreen
    Commented Apr 16, 2018 at 13:35
  • @EBGreen please see the update. I hope I have added enough details. I am new to powershell so your questions are helpful.
    – lemming622
    Commented Apr 16, 2018 at 14:08
  • 1
    So what code have you actually written so far?
    – EBGreen
    Commented Apr 16, 2018 at 14:10
  • @EBGreen My code looks like that of the link I included. I don't even know where to begin trying to add another field to the output. That's why I asked for help.
    – lemming622
    Commented Apr 16, 2018 at 17:34
  • You mean just Get-Counter "\Process(*)\Working Set - Private"? Because that doesn't really count as writing code. But, if that is all you have then that is all you have.
    – EBGreen
    Commented Apr 16, 2018 at 17:36

2 Answers 2

0

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
1
  • I tried the code you provided. With the get-date all that is presented in the current time. If I remove that section then I have no prompts or displayed information.
    – lemming622
    Commented Apr 16, 2018 at 21:35
0

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| Select-Object -First 15 | ft Name, PercentProcessorTime, IDProcess, "WSP(MB)" -AutoSize; start-sleep 5}

This is what I was looking for. Thank you @Clayton for a great starting point. Here is an explanation for what each part does.

while(1) => always running

cls => clear the current screen

Get-WmiObject => used to get all of the current process information

select-object => only select the columns of data that are wanted

? {$_.Name -notmatch "^(idle|_total|system)$"} => remove the process names that have idle, _total, or system in firs part of the process name

Sort-Object => sort the processes by cpu usage in descending order

Select-Object => select only the first 15 items (top 15 cpu processes)

ft => provide table formatting with column titles and using the whole screen

start-sleep => sleep the loop to running approximately every 5 sec

1
  • See my updated answer on how to properly report CPU utilization on multi core sytems...
    – Clayton
    Commented Apr 17, 2018 at 16:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .