Active Directory CSV of groups user is member of

$ADUserList = Get-ADUser -LDAPFilter ‘(!userAccountControl:1.2.840.113556.1.4.803:=2)’ | Select *
ForEach($ADProperties in $ADUserList)
{
$MemberOfGroups = $null
$MemberOfGroups = (Get-ADUser $ADProperties.SAMAccountName -Properties *).MemberOf
Write-Host ("Name: " + $ADProperties.Name + “,” + " Account: " + $ADProperties.SAMAccountName + “,” + " Groups: " + $MemberOfGroups)
}

This is my starting point but am not sure how to export to a CSV and make it a format I could import at a later date and apply to users.

Process is:

  1. New-ADUser -Name tom -Enabled $true -PasswordNotRequired $true
  2. Add-ADGroupMember -Identity TestGroup -Members tom

Not sure what format to use for CSV:
“User”,“Groups”
“tom”,@ArrayOfGroups

I’m not completely sure if I got what you’re trying to ask. If I had to create a list of all users and their groupmemberships I’d start with something like this:

$SearchBase = 'OU=Users,OU=Berlin,OU=Germany,OU=Europe,DC=contoso,DC=com'
$ADUserList = Get-ADUser -SearchBase $SearchBase -Filter * -Properties MemberOf
$Result =
foreach ($ADUser in $ADUserList) {
    foreach ($Group in $ADUser.MemberOf) {
        [PSCustomObject]@{
            ADUser = $ADUser.SAMAccountName
            Group  = $Group
        }
    }
}
$Result |
Export-Csv -Path '<path to a csv file of your choice>.csv' -NoTypeInformation -Delimiter ','

If you don’t want to limit yourself to a certain set of properties you could use Export-Clixml to preserve actually all properties you’ve got from the AD query.

$ADUserList | 
    Export-Clixml -Path '<path to an XML file of your choice>.xml'

Later on you could use Import-Clixml to re-import the data and use them as if you queried them just a second ago.

1 Like