aales1
aales1

Reputation: 3

Get list of Active Directory groups and its members regarding group description

I would like to get a list of Active Directory groups and their members (users) based on a group description (text). It would be great if the output file would be in the format:

Group1
User1
User2
User3
Group2
User2
User3
Group3
User1
.....

So far I got to the list of groups that contains text that is in the description. I was not able to get members of these groups.

Get-Adgroup -Filter * - Properties Name, Description | Select Name, Description | Where-Object {$_.Description -eq "description-text"} 

I did get a list of Groups (Name) and Description only containg Groups that have desired description. I tried to continue with | Get-AdGroupMember -Identity but did not get anywhere.

Upvotes: 0

Views: 1394

Answers (1)

Benjamin Hubbard
Benjamin Hubbard

Reputation: 2917

It's more efficient to filter closer to the left end of the pipeline as possible, i.e., for description. Try something like this:

# Gets all groups with the specific description
$Groups = Get-ADGroup -Filter "Description -like 'description-text'"
# Iterate through the list of groups
foreach ($Group in $Groups) {
    $Group.Name  # Outputs the group name
    $Users = $Group | Get-ADGroupMember  # Gets the list of users in that group
    $Users.Name  # Outputs all the users' names
}

Upvotes: 0

Related Questions