Share via

Help deleting members from Distribution Group using Azure Automation

Tyler Johnson 120 Reputation points
2026-03-09T12:15:56.35+00:00

Hello,

I have class lists where sometimes the child moves to a different class. The list updates on my computer automatically, this code takes that list and updates the distribution group with the newly added parents, but does not remove them from the old class(list) they were in.

Is there a way to tweak this code to make that happen or do I need to run separate code for that?

thanks!

#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 "Brown Parents"

#3. Import members from a CSV file
$memberList = Import-Csv "C:\Users\tylejohn\OneDrive - Alcuin School\Documents\Power Automate Lists\Brown.csv"

#4. Fetch current members (Email addresses) to compare against
$targetGroup = "Brown Parents"
$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)"
        }
    }
}

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 6,560 Reputation points Microsoft External Staff Moderator
    2026-03-09T15:55:08.9233333+00:00

    Hello Tyler Johnson, To remove the Parents from the Existing distribution group you can use the Remove-DistributionGroupMember, this will remove the parent from the existing group. For example.

    # 1. Connect to Exchange Online (Managed Identity)
    Connect-ExchangeOnline -ManagedIdentity -Organization "<Your Private Domain>"
    
    # 2. Define target Distribution Group identity
    $targetGroup = "Brown Parents"
    
    # 3. Import members from CSV (current class list)
    $memberList = Import-Csv "C:\Users\tylejohn\OneDrive - Alcuin School\Documents\Power Automate Lists\Brown.csv"
    
    # 4. Build a list of desired email addresses from the CSV
    $desiredMembers = $memberList.UserEmail
    
    # 5. Fetch current members (email addresses) in the DG
    $existingMembers = Get-DistributionGroupMember -Identity $targetGroup |
        Select-Object -ExpandProperty PrimarySmtpAddress
    
    # 6. ADD: members that are in CSV but not yet in the group
    foreach ($email in $desiredMembers) {
        if ($existingMembers -contains $email) {
            Write-Output "${email} is already a member of $targetGroup, skipping add."
        } else {
            try {
                Add-DistributionGroupMember -Identity $targetGroup -Member $email -ErrorAction Stop
                Write-Output "Successfully added ${email} to $targetGroup"
            } catch {
                Write-Warning "Failed to add ${email}: $($_.Exception.Message)"
            }
        }
    }
    
    # 7. REMOVE: members that are in the group but not in the CSV
    foreach ($email in $existingMembers) {
        if ($desiredMembers -notcontains $email) {
            try {
                Remove-DistributionGroupMember -Identity $targetGroup -Member $email -Confirm:$false -ErrorAction Stop
                Write-Output "Removed ${email} from $targetGroup because it is no longer in the class list."
            } catch {
                Write-Warning "Failed to remove ${email}: $($_.Exception.Message)"
            }
        }
    }
    

    Hope this helps! please let us know if you face any difficulties, thanks

    1 person found 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.