Due to the popularity of the initial script (cheers!) Azure AD – Export Groups and Members to CSV, and thanks to David for asking, this script will export the groups and the members with properties ObjectID, UserPrincipalName and Email Address. This one uses the AzAD cmdlets. To import them, use:
Install-Module Az -SkipPublisherCheck -Force -AllowClobber -Confirm:$false
Greetings! 👀 After a comment on my initial post asking for user email addresses in the output, I ended up getting a bit confused for 4 hours while trying to achieve the goal (it was a Friday night so several beers were involved) 🍻 !!
When I started seeing the dreaded pages of red errors in my results I soon realised I was not thinking that objects other than users can be members of a group. Of course! So I need to cater for computers, nested groups and users with no email address.
The result is below and from initial testing it seems to work well. Key points:
As with the original script, the CSV will output AD groups and members.
Where a group has no members, the group name is output with ‘No Members’ in the members column (and also now in the EmailAddress column).
The CSV has an ‘EmailAddress’ column added:
Where the member is a user and has an email address, the address is displayed.
Where the member is a user and does not have an address, ‘No Email Address’ is displayed.
Where the member is a computer, ‘Computer Object’ is displayed.
Where the member is a group, ‘Nested Group’ is displayed.
Voilà mes amis ! Code is below – as usual please comment if it helped or you made it better or it didn’t work for you ✌😃🤞. Thanks for coming, until nek tiya !
UPDATE Sept ’22 – David made me do it – well, he didn’t make me at all really but I did it anyway 🙂. Check out this new post which uses AzAD cmdlets to get the groups and members email, UPN and ObjectID: Azure AD – export groups and members #2.
# export azure ad groups and members to csv (also output empty groups with 'No Members' value)
# assumes existing connection to Azure AD using Connect-AzureAD (or use a runbook)
$allgroups = Get-AzureADGroup | select ObjectId,DisplayName
$result = foreach ( $group in $allgroups ) {
$hash = @{GroupName=$group.DisplayName;Member=''}
$groupid = $group.ObjectId
if ( $members = Get-AzureADGroupMember -ObjectId $groupid ) {
foreach ( $member in $members ) {
$hash.Member = $member.DisplayName
New-Object psObject -Property $hash
}
}
else
{
$displayname = "No Members"
$hash.Member = $displayname
New-Object psObject -Property $hash
}
}
$result | Export-Csv -Path C:\temp\AzureADGroups.csv -NoTypeInformation
# End
PowerShell get azure ad group members export to csv
export azure ad group members to csv PowerShell
PowerShell export azure ad user group membership to csv
recent comms…