Entra / Microsoft 365 · Teams
Convert dynamic distribution list to Microsoft 365 group
Converts a dynamic distribution list to a Microsoft 365 group and team-enables the new group.
Connect & set up
Run these once per session. All scopes are read-only unless the script makes changes.
Connect-ExchangeOnlineConnect-MgGraph -Scopes Directory.Read.All, User.Read.All
Run it
The main script. Copy it, or download the .ps1 and run it from your console.
$ModulesLoaded = Get-Module | Select NameIf (!($ModulesLoaded -match "ExchangeOnlineManagement")) {Write-Host "Please connect to the Exchange Online Management module and then restart the script"; break}# OK, we seem to be fully connected to Exchange Online.# Connect to the GraphConnect-MgGraph -Scope "Directory.Read.All, User.Read.All"$InputDDL = Read-Host "Enter the name of the Dynamic Distribution List to convert to a Microsoft 365 Group"[array]$SourceDDL = Get-DynamicDistributionGroup -Identity $InputDDL -ErrorAction SilentlyContinueIf (!($SourceDDL)) {Write-Host ("Sorry! We can't find the {0} dynamic distribution list" -f $InputDDL); break}If ($SourceDDL.Count -gt 1) {CLSWrite-Host "We found multiple matching dynamic distribution lists"Write-Host "-----------------------------------------------------"Write-Host " "$SourceDDL | Format-Table DisplayName, Alias, PrimarySMTPAddressWrite-Host " "Write-Host "Please try again..."; break }[string]$SourceDDLId = $SourceDDL.ExternalDirectoryObjectId# Now that we have a source DDL, let's get its membership[array]$SourceMembers = Get-Recipient -RecipientPreviewFilter (Get-DynamicDistributionGroup -Identity $SourceDDLId).RecipientFilter# could also be# [array]$SourceMembers = Get-DynamicDistributionGroupMember -Identity $SourceDDL.Id# Throw away anything but user mailboxes because that's all a Microsoft 365 group supports[array]$ValidMembers = $SourceMembers | ? {$_.RecipientTypeDetails -eq "UserMailbox"}# We've got to assign an owner to the new Microsoft 365 group, so we need to have a default in case the source DDL doesn't have an owner# Find the set of accounts that are Exchange admins (you can also use Get-AzureADDirectoryRoleMember here)[array]$ExoAdmins = Get-MgDirectoryRoleMember -DirectoryRoleId "53add08e-5b0c-4276-a582-9ce02fb6c947" | Select Id, AdditionalProperties# Throw away any service principals which might have the Exchange Admin role$ExoAdmins = $ExoAdmins | ? {$_.AdditionalProperties.'@odata.type' -eq '#microsoft.graph.user'} | Select -ExpandProperty Id# Select the first and use them as the default owner$ExoDefaultAdmin = Get-MgUser -UserId $ExoAdmins[0] | Select -ExpandProperty UserPrincipalName# Check that the group owner is a mailbox$GroupOwner = Get-ExoMailbox -Identity $SourceDDL.Managedby -ErrorAction SilentlyContinue# If it's null or something weird like a shared mailbox, use the default ownerIf (($GroupOwner -eq $Null) -or ($GroupOwner.RecipientTypeDetails -ne "UserMailbox")) {$GroupOwner = $ExoDefaultAdmin }Else {$GroupOwner = $GroupOwner.PrimarySmtpAddress}# Populate other group properties$AliasDDL = $SourceDDL.Alias + "M365"$GroupDisplayName = $SourceDDL.DisplayName + " (Group)"# Create the new Microsoft 365 GroupWrite-Host "Creating the new Microsoft 365 group..."$Description = "Created from the " + $SourceDDL.DisplayName + " dynamic distribution list on " + (Get-Date -Format g)# If you use senstivity labels, add an appropriate label by passing its identifier in the -SensitivityLabel parameter.$NewGroup = New-UnifiedGroup -DisplayName $GroupDisplayName –AccessType Private -Alias $AliasDDL -RequireSenderAuthenticationEnabled $True -Owner $SourceDDL.ManagedBy -AutoSubscribeNewMembers -Notes $Description# Add the members to the groupWrite-Host "Adding members from the dynamic distribution list to the Microsoft 365 group..."Add-UnifiedGroupLinks -Identity $NewGroup.ExternalDirectoryObjectId -LinkType Members -Links $ValidMembers.PrimarySmtpAddressWrite-Host "Enabing Microsoft Teams for the Microsoft 365 group..."New-Team -Group $NewGroup.ExternalDirectoryObjectIdWrite-Host "All done - new group and team created"
Attribution
Author
Office365itpros