Share via

Can Runbook script have multiple file lists in it?

Tyler Johnson 80 Reputation points
2026-03-02T15:21:12.74+00:00

Hello,

I'm back! haha. This script works to update a Distribution group from a list. However, I have like 20 different lists, so is there a way to add all the file paths in one code and it work?

I just tried running with 2 lists and it updated the 2nd list with one member in it, but it did not update and add the 2nd member in the original list...I'm missing something...

I added new code like this:


Get-DistributionGroup -Identity "JohnsonTest2"
Get-DistributionGroup -Identity "JohnsonTest3"

$memberList = Import-Csv "C:\Test\JohnsonTest2.csv"
$memberList = Import-Csv "C:\Test\JohnsonTes3.csv"


$targetGroup = "JohnsonTest2"
$targetGroup = "JohnsonTest3"
Azure Automation Runbook Code
#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"
#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)"
        }
    }
}
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,620 Reputation points Microsoft External Staff Moderator
    2026-03-02T16:09:33.86+00:00

    Hello Tyler Johnson, Thanks for the update, you can use the query below to process multiple files within a single folder to update the distribution list. Ensure that the target group name matches the file name.

    # Connect to Exchange Online
    Connect-ExchangeOnline -ManagedIdentity -Organization "<Your private Domain>"
    
    # Define (Group, CSV) pairs
    $FileLists = @(
        @{ Group = "JohnsonTest2"; CsvPath = "C:\Test\JohnsonTest2.csv" },
        @{ Group = "JohnsonTest3"; CsvPath = "C:\Test\JohnsonTest3.csv" }
    )
     
    foreach ($job in $FileLists) {
        $targetGroup = $job.Group
        $memberList  = Import-Csv $job.CsvPath
     
    # Get existing members for this group
        $existingMembers = Get-DistributionGroupMember -Identity $targetGroup |
                           Select-Object -ExpandProperty PrimarySmtpAddress
     
    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) to $targetGroup: $($_.Exception.Message)"
                }
            }
        }
    }
    

    Hope this helps. and please feel free to reach out if you encounter any issues. Thanks

    0 comments No comments

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.