Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like a single scripted line to export to CSV all the users in my AD with all the groups they are members of.

I'd prefer it to be done in powershell, but dsquery/dsget is a fine alternative really.

EDIT: i have trying to do something like this:

$user="administrator" (GET-ADUSER -Identity $user -Properties MemberOf | Select-Object MemberOf).MemberOf

But i'd like to have a loop of the $user variable to be changed by each line in reference text file.

This is the point were I don't know how to loop it.

share|improve this question
up vote 4 down vote accepted

Install the Quest CMDlets and then run this code:

Add-PSSnapin Quest.ActiveRoles.ADManagement
$memberships = @()

Get-QADGroup -SizeLimit 0 | Foreach-Object {
        $NameGroup = $_.Name
        Write-Host "Working with $NameGroup"
        $membership = Get-QADGroupMember $_.DN -Enabled -SizeLimit 0
        if ($membership -ne $null ) {
        $membership | Add-Member -type NoteProperty -name AuditGroupUserIsMemberOf -value $_.Name
        $memberships += $membership

$memberships | Select-Object AuditGroupUserIsMemberOf, NTAccountname | Export-Csv "GroupsWithUsers.csv"

This will give you a 1 record per group-user connection so expect multiple occurrences of users and groups. If you wan't other fields, you can just edit the Select-Object statement. Use $memberships | gm to see all the possibilities for the users. If you want more fields for the groups, use Get-QADGroup | gm, you will then need to add these by adding a new NoteProperty.

If you don't really care about more options, here is a one-liner you can just mash in the terminal:

Get-QADGroup -sizeLimit 0 | select @{name="Group";expression={$}} -expand members | select Group,@{n='User';e={ (Get-QADObject $_).NTAccountName}} | Export-Csv "MyUsersAndGroups.csv"
share|improve this answer
"Use $memberships | gm to see all the possibilities for the users." This Sentence here, im trying to understand what the GM function is doing? Please forgive my powershell noobish ways. – user131509 Aug 9 '12 at 6:25
gm stands for Get-Member it allows you to see all the properties and functions something else in powershell has. Try $String = "ABC" and then $String | gm So when you use $memberships | gm, it will show you all fields in the $memberships. – Bart De Vos Aug 9 '12 at 6:31
Thanks Mate! I'd like to actually have the OU listed as well. Would the GM do this function? – user131509 Aug 9 '12 at 6:56
Sure, Last line : Select-Object AuditGroupUserIsMemberOf, NTAccountname, add ,DN, after NTAccountName ;-) – Bart De Vos Aug 9 '12 at 7:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.