Share via

Distribution Lists

Glenn Maxwell 13,761 Reputation points
2021-07-18T08:45:54+00:00

Hi All

i have a distribution list and i want to add 5000 users to it. i have a csv file in the below format.
users
user1@Company portal .com
user2@Company portal .com

Some users will not get added since their email addresses are wrong as my csv file is not accurate. i want to get those users which were not added to the DL and those users which are added to the DL as a csv file.
i want to try something like the below but i am not sure.

$users = Import-CSV c:\temp\data.csv  
$mbxs = Get-Mailbox -ResultSize unlimited  
$output1 = foreach($mbx in $mbxs)  
{  
Get-Mailbox -Identity $mbx.Name  
}  
$output2 = foreach($user in $users)  
{  
if($output1.EmailAddresses -contains $user.user)  
{  
Add-DistributionGroupMember -Identity ******@contoso.com -Member $user.user  
Write-Host "Adding $user.user to Group" -ForegroundColor Green  
}  
else  
{  
Write-Host "Failed Adding $user.user to Group" -ForegroundColor Red  
}  
}  
Get-DistributionGroupMember -Identity ******@contoso.com | Export-csv c:\temp\op.csv  
Exchange Online
Exchange Online

A cloud-based service included in Microsoft 365, delivering scalable messaging and collaboration features with simplified management and automatic updates.

Exchange | Exchange Server | Management
Exchange | Exchange Server | Management

The administration and maintenance of Microsoft Exchange Server to ensure secure, reliable, and efficient email and collaboration services across an organization.

0 comments No comments

Answer accepted by question author

Kai Yao 37,791 Reputation points Moderator
2021-07-19T05:30:15.647+00:00

Hi @Glenn Maxwell

Did you mean you would like to add members to the distribution list using their email addresses, while you have some inaccurate information in the csv file.
For example, user1@Company portal .com no longer exists in your organization, but it is still in the csv file.

If I misunderstood your question, please feel free to correct me.


In this case, I suppose you may follow these steps:

1.Add the members to the distribution list

Import-CSV add_members.csv | ForEach {Add-DistributionGroupMember -Identity "DG1" -Member $_.PrimarySmtpAddress}  

The add_members.csv file is like:
PrimarySmtpAddress
user01@Company portal .com
user02@Company portal .com

2.Get the PrimarySmtpAddress value of the current members of the distribution list and export to a csv file named current_members.csv

Get-DistributionGroupMember -Identity ******@contoso.com | select-object PrimarySmtpAddress | export-csv current_members.csv  

The current_members.csv file would be like:
TYPE Selected.Microsoft.Exchange.Data.Directory.Management.ReducedRecipient
"PrimarySmtpAddress"
"user01@Company portal .com"
"user02@Company portal .com"

3.Use a Powershell script to compare the two csv files and export the results to a csv file named failed.csv (you need to run the script in Powershell instead of EMS)

  $file1 = import-csv -Path "C:\temp\add_members.csv"           
  $file2 = import-csv -Path "C:\temp\current_members.csv"          
  Compare-Object $file1 $file2 -property PrimarySmtpAddress | select PrimarySmtpAddress | Export-Csv -Path "C:\temp\failed.csv"  

You may check the failed.csv file to see the difference between the two csv files, which means users failed to be added.


If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.