UPDATE June ’22 – also check out Active Directory – export groups and members (with email addresses). It’s for on-premises AD but you will easily modify it for Azure AD I’m sure! Wont you? Then why haven’t I yet you say? 🤣😃🤣
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
this is awesome! is there a way we can import these users and groups with the data through Powershell?
Sure you can! Check out the Tenant Mailbox Migration post for an option to create users – https://www.howdoiuseacomputer.com/index.php/2022/04/02/microsoft-365-cross-tenant-migration
Create groups and add members using:
$csvdata = import-csv c:\temp\file.csv
foreach ($line in $csvdata) {
$groupdisplayname = $line.GroupName
$groupnickname = $line.GroupName -replace ‘[^a-zA-Z0-9]’, ”
$userprincipalname = $line.UserPrincipalName
if (!( Get-AzADGroup $groupdisplayname )) {
New-AzADGroup -DisplayName $groupdisplayname -MailNickname $groupnickname -GroupType Security
}
Add-AzADGroupMember -TargetGroupDisplayName $groupdisplayname -MemberUserPrincipalName $userprincipalname
}
Cheers
How can I add the User ID and/ or Userprincipal into the loop?
help very much appreciated.
Hi David, check out my other post https://www.howdoiuseacomputer.com/index.php/2022/04/02/export-active-directory-groups-and-members-to-a-csv-file-with-email-addresses.
It is for legacy AD but you can modify it for Azure using the “azAD” commands. Install the Azure modules: install-module az -skippublishercheck -force -allowclobber -confirm:$false
Here is an example of getting groups then members with Name and UPN:
$allgroups = Get-AzADGroup
foreach ( $group in $allgroups ) {
$groupid = $group.id
$groupdisplayname = $group.DisplayName
$members = Get-AzADGroupMember -GroupObjectId $groupid
foreach ( $member in $members ) {
$memberid = $member.Id
$userinfo = Get-AzADUser -ObjectId $memberid
$username = $userinfo.DisplayName
$upn = $userinfo.UserPrincipalName
Write-Host “$groupdisplayname,$username,$upn”
}
}
Cheers, Simon