Peter Hansen
Peter Hansen

Reputation: 81

Powershell Get ADGroupMember there is in 2 Identity groups

I want 1 list with all users there are member of 2 (both) identity.

I have used this, but it returns first all users in the first identity and then the next identity.

$groups = "SMSxxx", "Personalxxxx"
$results = foreach ($group in $groups) {
Get-ADGroupMember $group | select samaccountname, name, @{n='GroupName';e={$group}}, @{n='Description';e={(Get-ADGroup $group -Properties description).description}}
}
$results
$results | Export-csv C:\Temp\GroupMemberShip.txt -NoTypeInformation

Best regards, Peter

Upvotes: 0

Views: 329

Answers (1)

AdminOfThings
AdminOfThings

Reputation: 25061

You can continue with your current logic and use Group-Object to find users that exist in all groups.

$groups = "SMSxxx", "Personalxxxx"
$results = foreach ($group in $groups) {
    $description = (Get-ADGroup $group -Properties description).description
    Get-ADGroupMember $group | select SamAccountName,Name,@{n='GroupName';e={$group}}, @{n='Description';e={$description}}
}
$results | Group-Object SamAccountName |
    Where Count -eq $groups.Count | Select -Expand Group |
        Export-csv C:\Temp\GroupMemberShip.csv -NoTypeInformation

Upvotes: 1

Related Questions