Skip to content

Instantly share code, notes, and snippets.

@drtomasso
Created November 13, 2017 09:48
Show Gist options
  • Save drtomasso/a1be43f91803db084087b173b99f1b66 to your computer and use it in GitHub Desktop.
Save drtomasso/a1be43f91803db084087b173b99f1b66 to your computer and use it in GitHub Desktop.
Export AD users in OU as Y-axis, Groups as X-axis, marks field where user is member of group
#####
### ./ad_user_group_matrix.ps1
### Export users in OU as Y-axis, Groups as X-axis, marks field where user is member of group
#####
# Import Modules
import-module activedirectory
# Empty Array
$Array = @()
# Get your users
$Users = get-aduser -filter 'enabled -eq $true' -searchbase "ou=Users,ou=Contoso,dc=contoso,dc=local" -Properties samaccountname,name,description,userprincipalname,memberof,enabled
$Groups = get-adgroup -filter * -searchbase "ou=Groups,ou=Contoso,dc=contoso,dc=local"
# Loop through the users
foreach ($user in $users) {
# Make a new object with the findings
$NewObj = New-Object System.Object
$NewObj | Add-Member -type NoteProperty -Name "Name" -Value "$($User.name)"
$NewObj | Add-Member -type NoteProperty -Name "Description" -Value "$($User.description)"
$NewObj | Add-Member -type NoteProperty -Name "UPN" -Value "$($User.userprincipalname)"
# Loop through the groups to create the matrix
foreach ($group in $groups) {
If ((get-aduser $($User.samaccountname) -Properties memberof).memberof -contains $($group.DistinguishedName)) {
$NewObj | Add-Member -type NoteProperty -Name "$($group.name)" -Value "X"
} Else {
$NewObj | Add-Member -type NoteProperty -Name "$($group.name)" -Value ""
}
}
# Add the object to an array (one at a time)
$Array += $NewObj
} # End Loop
# Dump filled array to a file
$Array | export-csv "GrpMembers.csv" -NoTypeInformation -encoding "unicode"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment