Tabulate property values?

This topic contains 4 replies, has 4 voices, and was last updated by Profile photo of Peter Jurgens Peter Jurgens 1 year, 5 months ago.

  • Author
  • #25544
    Profile photo of Michael Hammond
    Michael Hammond

    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.

  • #25545
    Profile photo of Dave Wyatt
    Dave Wyatt

    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.

  • #25546
    Profile photo of Rob Simmers
    Rob Simmers

    Should be this easy:

    Get-ADUser -filter * -property title | Group-Object -Property Title -NoElement | Sort-Object -Property Count
  • #25550
    Profile photo of Michael Hammond
    Michael Hammond

    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!

  • #25616
    Profile photo of Peter Jurgens
    Peter Jurgens

    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.

You must be logged in to reply to this topic.