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. 

I hope this blog is helpful to manage distribution group membership. You can also generate a distribution group membership report to list all DLs and their members. 

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

by Kathy time to read: 3 min
0