How to use Group-Object cmdlet in PowerShell?

As the name suggests, Group-Object is useful to group similar properties.


Get-Service | Group-Object Status


Count Name          Group
----- ----          -----
160 Stopped         {AarSvc_8f3023, AdobeFlashPlayerUpdateSvc, AJR outer, ALG...}
130 Running         {AdobeARMservice, Appinfo, AudioEndpointBuilder, Audiosrv...}

The above output is grouped with the Status (“Stopped” and “Running”). There is a total of 160 services are in Stopped status and 130 are in Running status.

Similarly, you can filter the group with the starttype property.

Get-Service | Group-Object StartType


PS C:\WINDOWS\system32> Get-Service | Group-
Object StartType
Count Name                          Group
----- ----                          -----
197 Manual                          {AarSvc_8f3023, AdobeFlashPlayerUpdateSvc, A JRouter, ALG...}
84 Automatic                        {AdobeARMservice, AudioEndpointBuilder, Audi , AVP20.0...}
9 Disabled                          {AppVClient, NetTcpPortSharing, RemoteAccess , RemoteRegistry...}

If you need only Count and Name property use, − NoElement parameter.

Get-Service | Group-Object StartType -NoElement


Count      Name
-----      ----
197        Manual
84         Automatic
9          Disabled

If you need the values of the group, you first need to convert into HashTable as shown below.

Get-Service | Group-Object Status –AsHashTable -AsString


Name             Value
----             -----
Stopped          {AarSvc_8f3023, AdobeFlashPlayerUpdateSvc, AJRo uter, ALG...}
Running          {AdobeARMservice, Appinfo, AudioEndpointBuilder , Audiosrv...}

Now we are interested in services that are running, so the output of services will be stored in a variable and then we will use the “Running” value to retrieve the desired output.

$services = Get-Service | Group-Object Status -AsHashTable -AsString


PS C:\WINDOWS\system32> $services.Running
Status          Name                   DisplayName
------          ----                   -----------
Running         AdobeARMservice        Adobe Acrobat Update Service
Running         Appinfo                Application Information
Running         AudioEndpointBu...     Windows Audio Endpoint Builder
Running         Audiosrv               Windows Audio
Running         AVP20.0                Kaspersky Anti-Virus Service 20.0
Running         BFE                    Base Filtering Engine
Running         BITS                   Background Intelligent Transfer Ser...
Running         Bluetooth Devic...     Bluetooth Device Monitor
Running         Bluetooth OBEX ...     Bluetooth OBEX Service
Running         BrokerInfrastru...     Background Tasks Infrastructure Ser...
Running         Browser                Computer Browser
Running         BTAGService            Bluetooth Audio Gateway Service
Running         BthAvctpSvc            AVCTP service
Running         bthserv                Bluetooth Support Service
Running         camsvc                 Capability Access Manager Service
Running         cbdhsvc_8f3023         Clipboard User Service_8f3023
Running         CDPSvc                 Connected Devices Platform Service
Running         CDPUserSvc_8f3023      Connected Devices Platform User Ser...
Running         ClickToRunSvc          Microsoft Office Click-to-Run Service
Running         ClipSVC                Client License Service (ClipSVC)