Author Posts

May 21, 2015 at 11:24 am

I'm looking to be able to query a data source (Active Directory though any other should suffice), where I pull in the values in a property, and also calculate how many occurrences of each value are present in that list.

So assuming AD Users:

Name Title
_____ _____
Bob Manager
Alice Executive
Charlie Manager

I want to say something like Get-ADUser -filter * -property title | select-object title -unique | "magic happens here"

And get a result like:

Title Count
—— ——–
Manager 2
Executive 1

Any ideas? I tried "for-each"ing the unique list back into a retrieval from AD in a subexpression and then pulling out a count, but PowerShell just kept laughing and laughing at me.

May 21, 2015 at 11:44 am

That's more or less what Group-Object does:

Get-ADUser -Filter * -Property Title | Select-Object * | Group-Object -Property Title

I'm not sure if the "Select-Object *" part is necessary or not, but the AD cmdlets sometimes misbehave in a pipeline, so I put it there out of habit and caution.

May 21, 2015 at 11:48 am

Should be this easy:

Get-ADUser -filter * -property title | Group-Object -Property Title -NoElement | Sort-Object -Property Count

May 21, 2015 at 12:45 pm

Outstanding. I'm a little embarrassed to realize that Group-Object has been in PowerShell since version 3.0 and I didn't know...

Thanks all for your help!

May 25, 2015 at 4:14 am

Not to rub salt in the wound, but if I recall correctly group-object has been around since PS1.0... Don't be embarrassed, I'm continuously finding out new things with/about PowerShell. IT is a continuous learning experience. Once you consider yourself an expert, you've already fallen behind.