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
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
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
Hi,
Thanks for the above script
Can you please share the script, wherein we can get the email address details of the members.
Hi Shanmugavel, you gave me a good mission! 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/.
I hope it works for you 🙂
Cheers,
Simon (aka AdminDude)