Prof. Powershell

Group Hash

Group-Object, we hardly knew ye. One more tip before we close the book on you.

This time, we wrap up the short series on grouping in Windows PowerShell using the Group-Object cmdlet. But there's one more useful approach I want to make sure you learn. We have been working with GroupInfo object that Group-Object creates. However, you can instruct Group-Object to create a hash table instead:

PS C:\> $hash=Get-WmiObject -Class win32_service | Group-Object -Property StartMode -AsHashTable

I now have a hash table, also known as an associative array, of all services organized by their start mode:

PS C:\> $hash

Name     Value
----     -----
Manual   {\\SERENITY\root\cimv2:Win32_Service.Name="AeLookup...
Auto     {\\SERENITY\root\cimv2:Win32_Service.Name="AudioEnd...
Disabled {\\SERENITY\root\cimv2:Win32_Service.Name="cfWiMAXS...

Hash tables make it easier to work with the underlying connections. For example, I can easily count how many services are configured to autostart:

PS C:\> $

Getting all counts isn't that much more difficult:

PS C:\> $hash.GetEnumerator() | Format-Table -Property name,@{name="Count";Expression={$hash.item($ }} -auto

Name     Count
----     -----
Manual   103
Auto     66
Disabled 12

I use the GetEnumerator() method so that I get an object with properties to work with.

The hash table approach also makes it easier to get information about each group. For example, I wonder what services are configured as disabled?

PS C:\> $hash.disabled | Select Name


Or which services should be running automatically but aren't?

PS C:\> $ | where {$_.state -ne "running"} | select Name,StartMode,State

Name                           StartMode State
----                           --------- -----
clr_optimization_v4.0.30319_32 Auto      Stopped
clr_optimization_v4.0.30319_64 Auto      Stopped
MMCSS                          Auto      Stopped
sppsvc                         Auto      Stopped
WSearch                        Auto      Stopped

By grouping objects and creating a hash table I have a great deal of flexibility. Remember, I have the full WMI service object to work with. You might want to try some creative hashing of your own. Here are some ideas to get you started:

Get-WmiObject -Class win32_service | Group-Object -Property StartName –AsHashTable
Get-WmiObject -Class win32_service | Group-Object -Property State –AsHashTable
Get-WmiObject -Class win32_service -computername (get-content computers.txt) | Group-Object -Property SystemName -AsHashTable

Have fun and next time, we'll get, like, totally modular!

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular