Add a User to Multiple Distribution Lists in Office 365 using PowerShell 

Add a User to Multiple Distribution Lists in Office 365 using PowerShell

After reading a feature request on our ‘how to add bulk users to distribution list’ blog, I have decided to write a script to add a user to bulk distribution lists using PowerShell. This blog will guide you through multiple use cases, such as.

  • Add a user to bulk distribution groups
  • Add multiple users to bulk distribution groups
  • Remove a user from multiple distribution groups
  • Remove multiple users from several distribution groups.
  • Get all distribution lists a user is a member of.

All these use cases use input CSV files to provide bulk inputs. The scripts use the ‘Add-DistributionGroupMember’ and ‘Remove-DistributionGroupMember‘ PowerShell cmdlets. So, you must install the Exchange Online PowerShell module to run the scripts below.

Add a User to Bulk Distribution Groups:

When you want to add a user to several distribution groups, run the script below.

Connect-ExchangeOnline 
Import-CSV <GroupNamesCSV_FilePath> | foreach {  
 $GroupUPN=$_.GroupUPN 
 Write-Progress -Activity "Adding user to $GroupUPN… "  
 Add-DistributionGroupMember –Identity $GroupUPN -Member <UserUPN>  
 If($?)  
 {  
 Write-Host User successfully added to $GroupUPN -ForegroundColor Green  
 }  
 Else  
 {  
 Write-Host Error occurred while adding user to $GroupUPN –ForegroundColor Red  
 }  
} 

In the above script, you must replace the following.

<GroupNamesCSV_FilePath> – DL names CSV file path

<UserUPN> – User’s UPN which needs to be added in DLs

Add Multiple Users to Bulk Distribution Lists:

To add multiple users to multiple distribution groups, you must have 2 CSV files, one with user names and another with distribution group names.

Connect-ExchangeOnline 
$UserUPNs=Import-CSV <UserNamesCSV_FilePath> 
Import-CSV <GroupNamesCSV_FilePath> | foreach {  
 $GroupUPN=$_.GroupUPN 
 Foreach($UserUPN in $UserUPNs.UserUPN) 
 { 
  Write-Progress -Activity "Adding $UserUPN to $GroupUPN… "  
  Add-DistributionGroupMember –Identity $GroupUPN -Member $UserUPN  
  If($?)  
  {  
   Write-Host $UserUPN successfully added to $GroupUPN -ForegroundColor Green  
  }  
  Else  
  {  
   Write-Host Error occurred while adding $UserUPN to $GroupUPN –ForegroundColor Red  
  }  
 } 
} 

In the above script, you must replace the following.

<GroupNamesCSV_FilePath> – DL names CSV file path

<UserNamesCSV_FilePath> – User UPNs CSV file path

Sample Input CSV File:

Add users to bulk Distribution lists DL names - Input CSV format

Removing a User from Multiple Distribution Groups:

Admins can use the following code snippet to remove a user from bulk distribution lists.

Connect-ExchangeOnline 
Import-CSV <GroupNamesCSV_FilePath> | foreach {  
 $GroupUPN=$_.GroupUPN 
 Write-Progress -Activity "Removing user from $GroupUPN… "  
 Remove-DistributionGroupMember –Identity $GroupUPN -Member <UserUPN> -Confirm:$false 
 If($?)  
 {  
 Write-Host User successfully removed from $GroupUPN -ForegroundColor Green  
 }  
 Else  
 {  
 Write-Host Error occurred while removing user from $GroupUPN –ForegroundColor Red  
 }  
} 

In the above script, you must replace the following.

<GroupNamesCSV_FilePath> – DL names CSV file path

<UserUPN> – User’s UPN which needs to be removed from DLs

Removing Multiple Users from Bulk Distribution Lists:

To remove bulk users from multiple distribution lists, you can copy and execute the code.

Connect-ExchangeOnline 
$UserUPNs=Import-CSV <UserNamesCSV_FilePath> 
Import-CSV <GroupNamesCSV_FilePath> | foreach {  
 $GroupUPN=$_.GroupUPN 
 Foreach($UserUPN in $UserUPNs.UserUPN) 
 { 
  Write-Progress -Activity "Removing $UserUPN from $GroupUPN… "  
  Remove-DistributionGroupMember –Identity $GroupUPN -Member $UserUPN -Confirm:$false 
  If($?)  
  {  
  Write-Host $UserUPN successfully removed from $GroupUPN -ForegroundColor Green  
  }  
  Else  
  {  
  Write-Host Error occurred while removinging $UserUPN from $GroupUPN –ForegroundColor Red  
  }  
 } 
} 

In the above script, you must replace the following.

<GroupNamesCSV_FilePath> – DL names CSV file path

<UserNamesCSV_FilePath> – User UPNs CSV file path

Get All Distribution Lists a User is Member of:

After adding and removing groups from users, you might want to check the current distribution group membership of each user. You can download the PowerShell script to list all distribution groups a user is member of.

Sample Output:

List all distribution lists a user is member of

Script Highlights:

  • The script uses modern authentication to connect to Exchange Online.
  • The script can be executed with MFA enabled account
  • Automatically installs the EXO V2 module (if not installed already) upon your confirmation.
  • The script is scheduler-friendly. Credentials are passed as parameters, so worry not!
  • Allows generating user membership reports based on your requirement.
    • DL membership for all users.
    • DL membership for a list of users (import CSV).
    • DL membership for a single user.

Note: You can also generate and export distribution group membership report to list all DLs and their members.

Get a Step Closer to Distribution List Management Using AdminDroid

Apart from adding users to the multiple distribution lists in Office 365, it is essential to monitor them too! For better group management, the information provided by admin centers is insufficient to manage distribution groups efficiently. Although PowerShell provides sufficient details, it is a cumbersome and time-consuming process! But here is the possibility to easily view all the required details about Microsoft 365 groups at a glance!

AdminDroid’s Microsoft 365 group reports offer an elaborate and comprehensive view of distribution groups, security groups, nested groups, and every group in Microsoft 365. These reports go beyond basic information and provide relevant group membership info and group sizes. Overall, these reports help administrators analyze and optimize their Microsoft 365 group settings effectively.

List of all Office 365 Distribution Group in AdminDroid

Detailed Report on Distribution Group Members - AdminDroid

The free Azure AD reporting tool offers 75+Auditing Reports, 45+Statistical Reports, and 10+Insightful Dashboards. With this, admins can visualize a range of information, such as Office 365 user reports, license reports, license expiry reports, Microsoft 365 group reports, etc. Additionally, grab the entire visibility over Office 365 accounts without MFA, weak password users, Microsoft 365 admin roles, and more to prevent data threats and achieve productive Azure AD management.

Thus, use AdminDroid to elevate your Office 365 management game to the next level! Download the AdminDroid Microsoft 365 reporting tool now to explore all the features it offers.

I hope this blog is helpful to manage distribution group membership. Feel free to leave your queries in the comment section.

Add a User to Multiple Distribution Lists in Office 365 using PowerShell 

by Kathy time to read: 4 min
0