Skip to content

Instantly share code, notes, and snippets.

@Windos
Created December 11, 2019 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Windos/eaaf1298bd30845fb6e6d9423394d4f9 to your computer and use it in GitHub Desktop.
Save Windos/eaaf1298bd30845fb6e6d9423394d4f9 to your computer and use it in GitHub Desktop.
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