Share via

Help Updating Distribution Group with members already in it using Azure Automation Runbook

Tyler Johnson 80 Reputation points
2026-02-26T21:53:04.5+00:00

Hello,

I need to be able to create an Azure Automation Runbook that when new parents are added to my Excel list on my computer, it updates the Distribution Group.

The code below works, but only if there are no members in the distribution group. If the same person is in there already, the Runbook fails.

Is this a way to modify this script so that if it sees the same name, skip it, and add the new names?

# 1. Connect to Exchange Online (Use Managed Identity for Azure Automation)
Connect-ExchangeOnline -ManagedIdentity -Organization "org"

# 2. Define target Distribution Group identity
Get-DistributionGroup -Identity "JohnsonTest2"

# 3. Import members from a CSV file
$memberList = Import-Csv "C:\Test\JohnsonTest.csv"
Write-Output "member list $($memberList.count)"
# 4. Loop through the list and add members
foreach ($row in $memberList) {
    try {
        Add-DistributionGroupMember -Identity $row.GroupName -Member $row.UserEmail -ErrorAction Stop
        Write-Output "Successfully added $($row.UserEmail)"
    } catch {
        Write-Warning "Failed to add $($row.UserEmail): $($_.Exception.Message)"
    }
}

Azure Automation
Azure Automation

An Azure service that is used to automate, configure, and install updates across hybrid environments.

{count} votes

Answer accepted by question author
  1. Bharath Y P 5,540 Reputation points Microsoft External Staff Moderator
    2026-02-26T22:14:43.74+00:00

    Hello Tyler Johnson, we understand that you're Azure Automation Runbook that adds members to an Exchange Online Distribution Group, and the script fails when a person from your input list is already a member of the group. You want the runbook to skip existing members and only add new ones, so it doesn’t error out on duplicates.

    Your runbook fails because Add-DistributionGroupMember throws an error when you try to add a member who is already present in the distribution group. If you don’t handle that condition (by checking first or handling the error), the runbook treats it as a terminating failure and stops. The “check existing members and skip” logic avoids triggering that error path.

    You can try with the below script that can validate the existing member and skip the member that already exist.

    #1. Connect to Exchange Online (Use Managed Identity for Azure Automation)
    Connect-ExchangeOnline -ManagedIdentity -Organization "
    
    #2. Define target Distribution Group identity
    Get-DistributionGroup -Identity "JohnsonTest2"
    
    #3. Import members from a CSV file
    $memberList = Import-Csv "C:\Test\JohnsonTest.csv"
    
    #4. Fetch current members (Email addresses) to compare against
    $targetGroup = "JohnsonTest2"
    $existingMembers = Get-DistributionGroupMember -Identity $targetGroup | Select-Object -ExpandProperty PrimarySmtpAddress
    
    #5. Loop through the list and add members
    foreach ($row in $memberList) {
        if ($existingMembers -contains $row.UserEmail) {
            Write-Output "$($row.UserEmail) is already a member of $targetGroup, skipping."
        } else {
            try {
                Add-DistributionGroupMember -Identity $targetGroup -Member $row.UserEmail -ErrorAction Stop
                Write-Output "Successfully added $($row.UserEmail) to $targetGroup"
            } catch {
                Write-Warning "Failed to add $($row.UserEmail): $($_.Exception.Message)"
            }
        }
    }
    

    Hope this helps. and please feel free to reach out if you have any further questions. Thanks


1 additional answer

Sort by: Most helpful
  1. Q&A Assist
    2026-02-26T21:53:15.7+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    Use Update-DistributionGroupMember instead of looping with Add-DistributionGroupMember. That cmdlet replaces the entire membership list in one operation, so duplicates in the CSV do not cause failures and existing members are simply overwritten by the new full list.

    Example pattern:

    # 1. Connect to Exchange Online (Managed Identity)
    Connect-ExchangeOnline -ManagedIdentity -Organization "org"
    
    # 2. Import members from CSV
    $memberList = Import-Csv "C:\Test\JohnsonTest.csv"
    
    # 3. Build a unique list of member addresses from the CSV
    $members = $memberList.UserEmail | Sort-Object -Unique
    
    # 4. Update the distribution group membership in one shot
    Update-DistributionGroupMember -Identity "JohnsonTest2" -Members $members
    

    Key points:

    • Update-DistributionGroupMember is designed to replace all members of a distribution group or mail-enabled security group in one call.
    • The -Members parameter accepts a multivalued list (for example, email addresses) and does not error on existing members; it simply sets the membership to exactly what is passed.
    • Sort-Object -Unique ensures the list from the CSV has no duplicates before calling the cmdlet.

    If keeping some existing members that are not in the CSV is required, first read the current members, merge them with the CSV list, de-duplicate, and then pass the merged list to Update-DistributionGroupMember.


    References:

    0 comments No comments

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.