Arthur
Arthur

Reputation: 103

How can I get all of the members within one AD Group with many child groups by powershell?

I want to get all of the AD group members' uerprincipalname, but there has some many child group in it. I want to know how to do change the code below to achieve the goal. $UserList=Get-ADGroupmember -identity adgroup | % { get-aduser $_.samaccountname | select userprincipalname }

Upvotes: 0

Views: 488

Answers (1)

Theo
Theo

Reputation: 61068

If I understand the question correctly, your code almost does what you want, it now only also shows other AD objects, not only users.

If that is the case, you need to insert a Where-Object clause to filter out all no-user objects:

$UserList = Get-ADGroupmember -Identity adgroup -Recursive |
            Where-Object { $_.objectClass -eq 'user' } | ForEach-Object { 
                Get-ADUser -Identity $_.SamAccountName | Select-Object UserPrincipalName 
            }

This will return an array of objects. If what you need is an array of strings (just the UserPrincipalNames), add parameter ExpandProperty to the Select-Object:

Select-Object -ExpandProperty UserPrincipalName

P.S. If you only want to get a list of users that are direct member of the group, you can remove the -Recursive switch

Upvotes: 2

Related Questions