The Group-Object cmdlet allows you to group objects by their properties. This can be helpful when you want to display a large number of objects in the console.

When working with PowerShell, there are times when we need to work with sets of data. Normally, when working with objects in PowerShell, we're not just limited to only single objects. We work with lots of files, user accounts, virtual machines, and so on. When we display these various objects in the console, by default, the objects will just scroll down the screen one after the other with no regard for how you'd like to see them. Luckily, we can fix that.

Similar to the SQL GROUP BY statement, PowerShell has a way of summarizing objects by their properties. This allows the scripter to get a bird's eye view of lots of object properties at once. This grouping takes place by using the Group-Object command.

To explain what the Group-Object cmdlet does, let's generate a bunch of objects of the same type. These can be any objects, but let's just choose the System.ServiceProcess.ServiceController objects returned whenever you call Get-Service.

Note: For Group-Object to work as expected, make sure you're only grouping objects of the same type. It's important that all the objects have the same properties.

PS> $services = Get-Service

This returns all services on my local machine. Since there are a lot of services here, I can't easily get an idea of their status, startup type, and more. I'd like to group these services first by their status. To group these services, I'll pipe all the service objects to Group-Object and see what happens.

$services | Group-Object
Using Group Object

Using Group Object

Notice that by just piping all services to Group-Object, we can get the count. Big deal, but it's useful. To group them by a specific property (status in this case), I need to tell Group-Object I want to group on a particular object property. I do this by using the Property parameter on Group-Object.

$services | Group-Object -Property Status
Grouping by Property

Grouping by Property

Now we're talking! I can now see how many services are stopped and running at once. I can do the same for StartType as well.

$services | Group-Object -Property StartType
Grouping by Property #2

Grouping by Property #2

Maybe I want to dive in and see the actual services in one or more of these groups. I can get these objects by looking at the Group property that Group-Object returns. The Group object contains all the services that have the grouped-by object property value. To get all the stopped services if I'm grouping on service status, I can filter the services out by the status of Stopped and then expand the Group object to see all of those services.

$services | Group-Object -Property Status | Where {$_.Name -eq 'Stopped'} | Select -ExpandProperty Group

Not only can you group objects like this on a single property, but you can also group on multiple properties as well. Perhaps you'd like to see all the services based on their status and their start type. To do so, I just need to add another property name to the Property parameter on Group-Object.

Grouping by multiple properties

Grouping by multiple properties

You can see by adding an additional property, we can essentially create a bunch of "and" scenarios and group on as many properties as necessary!

1 Comment
  1. I know this cmdlet since the beginning, but most of the time I forget to use it...

    Thanks Adam for this reminder!

Leave a reply

Please enclose code in pre tags

Your email address will not be published. Required fields are marked *


© 4sysops 2006 - 2021


Please ask IT administration questions in the forums. Any other messages are welcome.


Log in with your credentials


Forgot your details?

Create Account