Understanding the PowerShell Group Object Command

Adam Bertram

Read more posts by this author.

When working with PowerShell, there are times when you need to work with sets of data. We work with lots of files, user accounts, virtual machines, and more. When you display these various objects in the console, the objects will scroll down the screen one after the other with no regard for how you’d like to see them. But that’s fixable! Using the PowerShell Group-Object cmdlet, you can group objects together. This cmdlet acts like the SQL GROUP BY statement.

PowerShell has a way of summarizing objects by their properties through the Powershell Group Object cmdlet. This cmdlet allows the scripter to get a bird’s eye view of lots of object properties at once in groups.

Let’s generate a bunch of objects of the same type. These objects can be of any type. However, for this demonstration, I’ll use System.ServiceProcess.ServiceController objects that the Get-Service cmdlet returns.

For Group-Object to work as expected, ensure that you’re only grouping objects of the same type. It’s important that all objects have the same properties.

PS51> $services = Get-Service
PS51> $services

Status   Name               DisplayName
------   ----               -----------
Stopped  AdtAgent           Microsoft Monitoring Agent Audit Fo...
Stopped  AJRouter           AllJoyn Router Service
Stopped  ALG                Application Layer Gateway Service

Group-Object with no Parameters

Get-Service returns all services on my local machine. Since there are a lot of services here, I can’t 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 service objects to PowerShell’s Group-Object cmdlet and use the Status property.

PS51> $services | Group-Object

Count Name                      Group
----- ----                      -----
  272 AdtAgent                  {AdtAgent, AJRouter, ALG, AppHostSvc...}

Notice that by piping all services to Powershell Group Object, you can get the count. Big deal but useful.

Grouping objects by a single property

To group them by a specific property (status in this case) I need to tell Group-Object that I want to group on a particular object property. That’s done by using the Property parameter on Group-Object.

PS51> $services | Group-Object -Property Status

Count Name                      Grou
----- ----                      -----
  160 Stopped                   {AdtAgent, AJRouter, ALG, AppIDSvc...}
  112 Running                   {AppHostSvc, Appinfo, Appveyor.Server, AudioEndpointBuilder...}

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.

PS51> $services | Group-Object -Property StartType

Count Name                      Group
----- ----                      -----
    9 Disabled                  {AdtAgent, AppVClient, NetTcpPortSharing, RemoteAccess...}
  184 Manual                    {AJRouter, ALG, AppIDSvc, Appinfo...}
   79 Automatic                 {AppHostSvc, Appveyor.Server, AudioEndpointBuilder, Audiosrv...}

Filtering Group-Object Output

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’s returned by the Powershell group object command. The Group object contains all of the services that have the grouped by object property value. To get all of 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.

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

Status   Name               DisplayName
------   ----               -----------
Stopped  AdtAgent           Microsoft Monitoring Agent Audit Fo...
Stopped  AJRouter           AllJoyn Router Service
Stopped  ALG                Application Layer Gateway Service
Stopped  AppIDSvc           Application Identity
Stopped  AppMgmt            Application Management
Stopped  AppReadiness       App Readiness

Grouping objects by multiple properties

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 of 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.

PS51> $services | Group-Object -Property Status,StartType

Count Name                      Group
----- ----                      -----
    9 Stopped, Disabled         {AdtAgent, AppVClient, NetTcpPortSharing, RemoteAccess...}
  145 Stopped, Manual           {AJRouter, ALG, AppIDSvc, AppMgmt...}
   73 Running, Automatic        {AppHostSvc, Appveyor.Server, AudioEndpointBuilder, Audiosrv...}
   39 Running, Manual           {Appinfo, camsvc, CertPropSvc, ClipSVC...}
    6 Stopped, Automatic        {gpsvc, MapsBroker, sppsvc, TrustedInstaller...}

You can see by adding an additional property; you can essentially create a bunch of “and” scenarios and group on as many properties as necessary!


The Group-Object cmdlet is a cmdlet that helps you group like objects together based on a common property. Grouping objects like this comes in handy in many different ways. I hope by learning a little about how the Group-Object cmdlet works, you’ll get more ideas on how to improve and create better PowerShell scripts!

If you’re just getting started with PowerShell, I highly encourage you to check out my mini-course on PowerShell tool-building!

Subscribe to Stay in Touch

Never miss out on your favorite ATA posts and our latest announcements!

Looks like you're offline!