Add users to DL

Rising Flight 4,596 Reputation points
2021-04-13T03:18:49.203+00:00

Hi Experts

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

i can add the users if i use the below syntax.
Import-CSV C:\list.csv | ForEach {Add-DistributionGroupMember -Identity dl1@Company portal .com -Member $_.users}

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 log file. how can edit the query and add it. i want to try something like the below but i am not sure.

Write-Host "Adding $users to Group"
Write-Host "Failed Adding $users to Group"

Microsoft Exchange Online Management
Microsoft Exchange Online Management
Microsoft Exchange Online: A Microsoft email and calendaring hosted service.Management: The act or process of organizing, handling, directing or controlling something.
4,558 questions
Exchange Server Management
Exchange Server Management
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Management: The act or process of organizing, handling, directing or controlling something.
7,653 questions
0 comments No comments
{count} votes

Accepted answer
  1. Xzsssss 8,871 Reputation points Microsoft Vendor
    2021-04-14T02:57:48.427+00:00

    Hi @Rising Flight ,

    Based on AshokM, this could export the success/error messages to a csv file:

    Import-Csv C:\list.csv |  
    ForEach{  
    try {  
    Add-DistributionGroupMember -Identity dl1@contoso.com -Member $_.users -ErrorAction Stop  
    Write-Output "Added $_ to Group" | Out-file "c:\import.csv" -Append  
    }  
    catch [System.Exception]  
    {  
    Write-Output "$_" | Out-File "c:\error.csv" -Append  
    }  
    Finally  
    {  
    }}  
    

    Example:
    87574-image.png
    Regards,
    Lou


    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 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Ashok M 6,516 Reputation points
    2021-04-13T05:53:20.11+00:00

    Hi @Rising Flight ,

    Try the below command and see if that works,

    $DGroup = Read-Host 'Insert Distribution Group Name'
    Import-Csv 'C:\Softlib\Users.csv' |
    ForEach{
    try {
    Add-DistributionGroupMember -Identity $DGroup -Member $.name -ErrorAction stop -WhatIf
    }
    catch [system.exception]
    {
    Write-Host "$
    .name" -ForegroundColor Red
    }
    Finally
    {
    }}

    https://techcommunity.microsoft.com/t5/office-365/bulk-add-user-to-distribution-group/m-p/66309
    https://community.spiceworks.com/topic/2153253-add-aduser-to-distribution-list-from-csv-with-success-fail-log
    https://community.spiceworks.com/topic/2259834-how-to-handle-error-in-powershell-when-adding-members-in-bulk-to-a-dl

    If the above suggestion helps, please click on "Accept Answer" and upvote it.

    0 comments No comments

Your answer

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