Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select top 5 items from each group using PowerShell

Tags:

Is there any 1-2 lines solution to select the top five items from each group using PowerShell?

For example, group processes returned by Get-Process by name and see the first three wp3 processes.

Of course I can iterate by each unique name, but I hope there is a shorter solution.

like image 557
Alex Blokha Avatar asked Mar 26 '14 18:03

Alex Blokha


1 Answers

Here you are, using Get-Process as you did previously.

Get-Process | Group-Object ProcessName | ForEach-Object {     $_ | Select-Object -ExpandProperty Group | Select-Object -First 5 } 

That will group things by property, and then for each group re-expand the group to its original form, and select the first 5 entries.

I suppose you could sort in there too before the Select-Object -First 5 pipe to only get the top CPU usage properties for it or something too and not just 5 seemingly random ones.

like image 176
TheMadTechnician Avatar answered Oct 01 '22 13:10

TheMadTechnician