O'Reilly logo

Pro Windows PowerShell by Hristo Deshev

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

3.8. Grouping Objects

Dealing with many objects often involves classifying them into several groups. PowerShell makes that very easy to do with its Group-Object cmdlet. The simplest and most common scenario is to split a collection into groups according to a property value. Here is how we can get a report of all services split into groups according to their status:

PS> Get-Service | Group-Object Status

Count Name                      Group
----- ----                      -----
   73 Running                   {AeLookupSvc, Appinfo, AudioEndpointBuil...
   79 Stopped                   {ALG, AppMgmt, aspnet_state, Browser...}

The result is a collection of GroupInfo objects. We can get each of them and query it for the actual objects that belong to the group by accessing the GroupInfo Group property:

PS> (Get-Service | Group-Object ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required