Prof. Powershell

Group Play with Group-Object

Gather and filter until you get the info you need. It's what the Group-Object cmdlet is all about.

Often in Windows PowerShell you need to organize information to make it more meaningful. While a directory listing is nice, perhaps you need to know what types of files are in the directory.

One approach is to group the results from Get-ChildItem (or DIR) by using Group-Object:

Get-childitem c:\work | where {-not $_.PSIsContainer} | group-object -property Extension

The Where-Object part of the expression is to filter out folder objects. Even though this expression start out with file items, the end results is a GroupInfo object. You should get output like this:

Count Name Group
----- ---- -----
    4 .lnk {adsvw.lnk, demo.lnk, foo2.lnk, sample.lnk}
   10 .csv {April2009_8596.csv, August2009_8596.csv, De...
    1 .hta {beg7.hta}
    5 .ps1 {beg8.ps1, contest.ps1, join-objectdev.ps1, ...
    2 .htm {brain.htm, cmdletinfo.htm}
    3 .zip {brain.zip, Microsoft.Exchange.Management-He...
    5 .txt {data.txt, foo.txt, foo2.txt, mdttest01.txt...}
    1 .bat {filemove.bat}
    3 .xml {foo.xml, Microsoft.Exchange.Management-Help...

The GroupInfo object has the three properties you see here, Count,Name and Group. Why is this important? Because you can pipe this object to other expressions:

PS C:\> Get-childitem c:\work | where {-not $_.PSIsContainer} | group-object -property Extension | Sort Count -Descending | Select -first 3

Count Name Group
----- ---- -----
   10 .csv {April2009_8596.csv, August2009_8596.csv, Decembe...
    8 .JPG {IMG_1375.JPG, IMG_1376.JPG, IMG_1377.JPG, IMG_13...
    5 .txt {data.txt, foo.txt, foo2.txt, mdttest01.txt...}

Now I have the top three file extensions. I have 10 CSV files in C:\Work. That information might be helpful. If I want to further slice and dice this information I'd likely save everything to a variable so I don't have to keep re-running the command:

PS C:\> $extensions=Get-childitem c:\work | where {-not $_.PSIsContainer} | group-object -property Extension | Sort Count -Descending

With this I can see the corresponding CSV files:

PS C:\> $extensions[0].Group
Directory: C:\work

Mode  LastWriteTime     Length Name
----  -------------     ------ ----
-a--- 2/24/2010 7:53 PM   5480 April2009_8596.csv
-a--- 2/24/2010 7:54 PM   5741 August2009_8596.csv
-a--- 2/24/2010 7:55 PM   8961 December2009_8596.csv
-a--- 2/24/2010 7:54 PM   8348 July2009_8596.csv
-a--- 2/24/2010 7:53 PM   8435 June2009_8596.csv
-a--- 2/24/2010 7:53 PM   6884 March2009_8596.csv
-a--- 2/24/2010 7:53 PM   7879 May2009_8596.csv
-a--- 2/24/2010 7:55 PM   4261 November2009_8596.csv
-a--- 2/24/2010 7:54 PM   6483 October2009_8596.csv
-a--- 2/24/2010 7:54 PM   5308 September2009_8596.csv

Finally, I can measure the group object:

PS C:\Scripts> $extensions | Measure-object -Property Count -sum

Count    : 24
Average  :
Sum      : 65
Maximum  :
Minimum  :
Property : Count

I see that there are 24 different file types in my folder and a total of 65 files. Hmm...I wonder what the are?

PS C:\Scripts> $extensions | sort Name | select name

Name
----
.application
.bat
.clg
.csv
.dll
.exe
.gif
.hta
.htm
.InstallLog
.InstallState
.JPG
.lnk
.log
.pcv
.pdf
.pptx
.ps1
.txt
.vb
.wav
.wim
.xml
.zip

Are you ready to join the group?

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 Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular

Upcoming Training Events

0 AM
Live! 360 Orlando
November 17-22, 2024
TechMentor @ Microsoft HQ
August 11-15, 2025