Export Active Directory groups and members to CSV file

UPDATE 2022-04-02 – if you would like email addresses with the output, check out my new post at: https://www.howdoiuseacomputer.com/index.php/2022/04/02/export-active-directory-groups-and-members-to-a-csv-file-with-email-addresses/

# export active directory groups and members to csv (also output empty groups with 'No Members' value)
# assumes run on domain controller or import of ActiveDirectory module

$allgroups = Get-ADGroup -Filter *

$result = foreach ( $group in $allgroups ) {

    $hash = @{GroupName=$group.SamAccountName;Member=''}
    $groupid = $group.distinguishedname
    
    if ( $members = Get-ADGroupMember $groupid ) {
            
            foreach ( $member in $members ) {

                $hash.Member = $member.Name
                New-Object psObject -Property $hash
            }
    }
    else {
        $displayname = "No Members"
        $hash.Member = $displayname
        New-Object psObject -Property $hash
    }
}

$result | Export-Csv -Path C:\temp\ActiveDirectoryGroupsAndMembers.csv -NoTypeInformation

# End

Loading

4 thoughts on “Export Active Directory groups and members to CSV file”

  1. Shanmugavel

    Hi Simon,
    Thank you very much, i just saw the post only today, one more doubt, why this is not including the disabled users OU

    1. Hi! The output would include any disabled users as there is no logic to exclude them… it would be easy to add to the email address script since that uses Get-AdUser.

      Is that what you mean?

      Cheers,
      Simon

  2. Hi,
    Thanks for the above script
    Can you please share the script, wherein we can get the email address details of the members.

Leave a Reply to simon.burbery Cancel Reply

Your email address will not be published. Required fields are marked *

Scroll to Top