Simple PowerShell Script to create a list with AD Users and their Groups

Had a request to create a list that shows all users of a given Active Directory Organizational Unit, and their group memberships.

Output is in CSV.
$allusers = Get-ADUser -SearchBase "OU=SubOU,OU=OU1,DC=dom,DC=com" -filter * -Properties * | Select-Object samaccountname, distinguishedname
"sep=;" | Out-File c:\temp\usertobundle.csv
foreach($myuser in $allusers)
{
$my_groups = Get-ADPrincipalGroupMembership $myuser.distinguishedname | select-object SamAccountName
foreach ($my_group in $my_groups)
{
$myuser.samaccountname + ";" + $my_group.SamAccountName | Out-File -Append c:\temp\usertobundle.csv
}
}

Not much more to say, did the job.

 

Leave a Reply