question

GlennMaxwell-2309 avatar image
0 Votes"
GlennMaxwell-2309 asked emilyhua-msft edited

Distribution Lists

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@contoso.com
user2@contoso.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 d1@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 d1@contoso.com | Export-csv c:\temp\op.csv
office-exchange-server-administrationoffice-exchange-online-itprooffice-exchange-server-mailflow
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

KaelYao-MSFT avatar image
0 Votes"
KaelYao-MSFT answered KaelYao-MSFT commented

Hi @GlennMaxwell-2309

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@contoso.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@contoso.com
user02@contoso.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 DG1@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@contoso.com"
"user02@contoso.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.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @GlennMaxwell-2309

I am writing here to confirm with you how thing going now?
Do suggestions above help? If you have any questions or needed further help on this issue, please feel free to post back.

0 Votes 0 ·