How to Populate Team or Group Membership from Email Distribution Lists

A question on the Microsoft Technical Community asked whether it’s possible to add the members from a mail group to a team. Taking “mail group” to mean “distribution list,” the answer is “of course with PowerShell.”

The big gotcha to remember is that distribution lists allow any mail-enabled recipient to be a member while Office 365 Groups and Teams only support mailboxes. Any code must therefore exclude objects like shared mailboxes, public folders, and mail contacts.

Read DL, Write Group

With that in mind, here’s some code to read the membership from a distribution list and add the mailboxes found in the list to an Office 365 group. As Teams and Groups share the same membership, the new members show up in Teams too. As written, the code uses cmdlets in the Exchange Online PowerShell module.

$i = 0
$Members = (Get-DistributionGroupMember -Identity "Company Sales")
$TeamId = (Get-UnifiedGroup -Identity "Sales Team").ExternalDirectoryObjectId
Write-Host "Processing Members"
ForEach ($M in $Members) {
  # Is this a mailbox
  If ($M.RecipientTypeDetails -eq "UserMailbox") {
    Write-Host "Adding" $M.DisplayName "to Sales Team"
    Add-UnifiedGroupLinks -Identity $TeamId -LinkType Member -Links $M.Alias
    # $UPN = (Get-Mailbox -Identity $M.Alias).UserPrincipalName
    # Add-TeamUser -GroupId $TeamId -User $UPN -Role Member
    $i++ }

Using the Teams PowerShell cmdlets

The two commented-out lines perform the update using the Add-TeamUser cmdlet, which is equally valid. However, remember to:

  • Load the Teams PowerShell module.
  • Pass the User Principal Name for the new team member (that’s why the call to Get-Mailbox is present).
  • Only team owners can update team membership. This restriction is likely to be removed soon, but it exists now.

Dynamic Groups

The note in the Microsoft Technical Community said that people spend a lot of time updating the distribution group membership. One response recommended dynamic groups, which is a perfectly valid option. The only downside is that you still need to maintain the directory so that the queries used for the dynamic groups return the right data. And you need Azure AD Premium P1 licenses for every account that comes within the scope of a query used for a dynamic group. If you have EM&S licenses, you don’t need to worry about Azure AD Premium as these licenses are included in that bundle.

Read more about using PowerShell with Office 365 Groups (and Teams) in Chapter 14 of the Office 365 for IT Pros eBook. Distribution lists are covered in Chapter 7.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.