gritts
gritts

Reputation: 185

How can I use powershell to group process names and show the sum of memory used

I am trying to wrap my head around combining powershell options in order to produce a simple table of the top 10 memory users on my system (server, pc, etc). My PC is Windows 7 with no timeline in site for upgrade to Windows 10 due to Covid 19. I cannot add applications to my work PC that has not gone through a vetting process (read, it takes forever) so most of the time I create my own.

I would like to produce a result that looks something like this:

Count Name Memory Sum in MB


10 Firefox 5000 3 javaw 1000

The order I would like to be able to select by changing a property in the powershell options. So for example, sort by count, name or memory. My sample table is not set in stone.

I have come across the following 2 pieces of powershell and have been trying to adapt them but get errors.

(Get-Process | Measure-Object WorkingSet -sum).sum /1gb

 Get-Process | Group-Object -Property Name -NoElement | Where-Object {$_.Count -gt 1}

For sake of learning, I don't mind seeing an "ugly" version and an optimized version.

Upvotes: 1

Views: 951

Answers (2)

JosefZ
JosefZ

Reputation: 30153

The PSCustomObject accelerator was introduced in PowerShell v3 so I don't know if the the output looks like a table in Windows 7 however the following pipeline returns desired properties even in PowerShell v2:

Get-Process |
  Group-Object -Property Name -NoElement |
    Where-Object { $_.Count -gt 1 } |
      ForEach-Object {
        [PSCustomObject]@{
            Count= $_.Count
            Name = $_.Name
            'Memory Sum in MB' = [math]::Round(( Get-Process -Name $_.Name |
                Measure-Object WorkingSet -sum).sum /1Mb, 3)
        }
      }    # | Sort-Object -Property 'Memory Sum in MB'

Upvotes: 1

wasif
wasif

Reputation: 15488

You can use this:

$proc=ps|select -eXp name;$proc2=@()
$proc|%{
if(!("$($_)" -in $proc2)){$proc2+="$($_)"
$mem=0;ps $_|select -eXp workingSet|%{$mem+=$_/1MB}
[pscustomobject][ordered]@{
'Count'=(ps $_ -ea silentlyContinue).Count
'Name'=$_
'Memory in MB'=$mem
}}}

Upvotes: 1

Related Questions