Powershell - Group-Object Cmdlet



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

Example 1"
Get-Service | Group-Object Status

Output

Count Name        Group
----- ----        -----
160 Stopped      {AarSvc_8f3023, AdobeFlashPlayerUpdateSvc, AJRouter, 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 in Stopped status and 130 in Running status.

Example 2"

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

Get-Service | Group-Object StartType

Output

Count Name        Group
----- ----        -----
197 Manual       {AarSvc_8f3023, AdobeFlashPlayerUpdateSvc, AJRouter, ALG...}
84 Automatic     {AdobeARMservice, AudioEndpointBuilder, Audiosrv, AVP20.0...}
9 Disabled       {AppVClient, NetTcpPortSharing, RemoteAccess, RemoteRegistry...}

Example 3"

If you need only Count and Name properties, use the -NoElement parameter.

Get-Service | Group-Object StartType -NoElement

Output

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

Example 4"

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

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

Output

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

Example 5"

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
$services.Running

Output

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)
Advertisements