Group membership query by group name

Welcome Forums General PowerShell Q&A Group membership query by group name

This topic contains 3 replies, has 3 voices, and was last updated by

1 year, 1 month ago.

  • Author
  • #85328

    Points: 53
    Rank: Member

    I'm trying to expand on some things I learned in this post and apply it to a group membership query

    What I eventually want is an output that looks something like this



    Or even

    Groups Users
    ———— ————
    Group1 Collection of users
    Group2 Collection of users

    So far I have written this:

    $groups = Get-AdGroup -filter {name -like "SP*"} -SearchBase $ou | select name
    Foreach ($group in $Groups) {
     $props =  [ordered]@{
                 'Group' = $($
                 'Users' = Get-ADGroupmember -Identity $($ -Recursive | select -ExpandProperty SamAccountName
    New-Object -Type PSObject -prop $props
    $props | -ft auto 

    Which gives me an output like
    Group Users
    —– —–
    Group1 {User1, User2, User3, User4...}
    Longgroupname... {User1, User2, User3, User4...}

    However I don't know how to expand it so it shows the full name of the group or all the items in users. I've played around with wrap, autosize, setting the $formatenumerationlimit to -1, which expanded some of the users but still not all of them.

  • #85330

    Points: 1,785
    Helping HandTeam Member
    Rank: Community Hero

    The way your'e doing it, it can't. When PowerShell needs to display a collection of items in a single property, it uses the {} syntax you're seeing. That's entire by design. If you want something else – what, a comma-separated list? – you have to do that yourself.

    'Users' = (Get-ADGroupmember -Identity $($ -Recursive | 
    select -ExpandProperty SamAccountName) -join ","

    Or instead of , perhaps use `n if you want carriage returns.

    For example. This makes the collection of discrete values into one giant string, with names separated by commas.

    A LOT of people run into this. The ultimate problem is that you're using a flat data display – a table, which is not unlike a CSV – to display hierarchical data. Just wasn't designed for it. PowerShell isn't really about _visualizing_ data – it's more about _working_ with data that is structured in the form of objects. So if you're trying to do something weird for a display, you're on your own to code it.

  • #85334

    Points: 53
    Rank: Member

    Well it's good to know a lot of people run into this, I was beginning to think I am crazy 😉

    I think I am going to stick to what I learned in the other thread you helped me in. I am not much of a coder, I'm a sys admin who gets stuck into getting this kind of stuff because I'm the only one in my group who is comfortable in powershell. I've been trying to be nice and give people stuff formatted nicely. However if this is a limitation, they can deal with it 🙂

  • #85345

    Points: 512
    Helping Hand
    Rank: Major Contributor

    This is a question similar to one recently posted that I responded to.
    See this thread....

    Show User and AD group membership

    ... to determine if you can leverage what I posted to get you to where you need to go.

The topic ‘Group membership query by group name’ is closed to new replies.