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.