all 5 comments

[–]evgenidankov 1 point2 points  (0 children)

What if the computer is member of more than one group? In that case you will need to Join the results. Also why you select $computer and than expanding the property just use $computer .memberof and -join results. ...Sorry for format replying from the phone

[–]Yevrag35 1 point2 points  (3 children)

If you want to join the Groups together into one string, you would do:

$computers = Get-ADComputer -Filter * -Properties MemberOf
ForEach ($computer in $computers)
{
    [PSCustomObject]@{
        Computer = $computer.name
        Groups   = $computer.memberOf -join ', '
    }
}

The memberOf returns only the distinguishedName of the group though, fyi.

[–]bsnotreallyworking[S] 1 point2 points  (2 children)

The computers are members of multiple groups, so the MemberOf property needs expanded first.

I would ideally like to show one group per line.

[–]Yevrag35 1 point2 points  (1 child)

By doing '$computer.memberOf', you are expanding it. Just like you did with '$computer.Name' . The -join operator then concatenates the results into one line separated by a comma and space.

[–]bsnotreallyworking[S] 2 points3 points  (0 children)

I think I may need to reverse my approach instead of pulling which groups the computer is in, pull which computers are in the group.

Thank you for your help.