Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Group Membership Matrix Example
$Users = Get-ADUser -Filter 'Enabled -eq $true'
$GroupNames = (Get-ADGroup -Filter * |
Where-Object {$_.DistinguishedName -notlike '*CN=Builtin,*'}).Name
$Report = foreach ($User in $Users) {
$Groups = Get-ADPrincipalGroupMembership -Identity $User.DistinguishedName
$ReportProp = [Ordered] @{
'Name' = $User.DisplayName
'Username' = $User.SamAccountName
'OU' = $User.DistinguishedName -replace '(CN=[\w\s-]+,){1}' , ''
}
foreach ($Group in $GroupNames) {
$Present = $Groups | Where-Object {$_.Name -eq $Group}
if ($Present) {
$ReportProp[$Group] = 'Member'
} else {
$ReportProp[$Group] = ''
}
}
[PSCustomObject] $ReportProp
}
$Report | Export-Csv -Path 'C:\Support\GroupMembershipMatrix.csv' -NoTypeInformation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.