Powershell 2: Add multiple members to distribution group with one call?

Assume I have a distribution group in Exchange which currently exists, and holds about 20 members.

In Powershell 2, I have acquired a list of about seven hundred more individuals who need to be added to the group. The only way I have been able to do this is one user at a time:

Add-DistributionGroupMember -Idneity GROUP-NAME -Member Member1
Add-DistributionGroupMember -Idneity GROUP-NAME -Member Member2
....
Add-DistributionGroupMember -Idneity GROUP-NAME -Member MemberN

Is there a mechanism available that allows me to add all users with just one function call, as represented by the following pseudo-code?

SOME-CMDLET-TO-ADD-LOTS-AT-ONCE -Identity GROUP-NAME -Member COLLECTION-OF-PEOPLE

I'm not looking for loop constructs. I'm looking for a function or cmdlet that can add all members at once, ideally taking only one argument referencing the entire list. I'm scared to think what Powershell would do with 700+ command-line arguments...

(I'm hoping that if this IS possible, that I can pass an array or collection of objects to be added.)

Any suggestions? I don't see a command called 'set-distributiongroupmember', and I also don't see anything promising in 'set-distributiongroup'.


I'm not sure why you don't want to use a loop construct; that's kinda one of the major features of PS. A simple:

Import-CSV FileName.csv | ForEach {Add-DistributionGroupMember -Identity "GROUP-NAME" -Member $_.Name}

would easily do the trick. Failing that, though, there's no cmdlet to do exactly what you want. You could do a

Remove-DistributionGroup GROUP-NAME; New-DistributionGroup -Name GROUP-NAME -Members memberlist

but that's a little cheesy.


I know this is an old post, but it's still the #1 result on Google when trying to find how to add multiple users to a DL so I figured I'd post my solution.

The previous solutions will work fine for a small list of users, but as the original poster stated there are performance and scaling issues with calling Add-DistributionGroupMember for every member when dealing with large membership changes, and this is compounded when dealing with O365.

To solve these issues you can use Update-DistributionGroupMember to replace the entire membership of a DL instead of adding them one by one.

e.g. for the original request you would do this:

Update-DistributionGroupMember -Identity GroupName -Members user1@domain,user2@domain,usern@domain

or

Update-DistributionGroupMember -Identity GroupName -Members $ArrayOfMemberEmailAddresses

This does replace the membership of the DL, so if you wanted to maintain the existing membership you would need to retrieve it first using Get-DistributionGroupMember then create an array of the existing members and new ones and give that to Update-DistributionGroupMember like the following:

$NewMembers = Get-Content <path to member list text file>
$CurrentMembers = (Get-DistributionGroupMember -Identity GroupName).PrimarySmtpAddress | where {$_ -ne ""}
$NewMemberList = $CurrentMembers + $NewMembers
Update-DistributionGroupMember -Identity GroupName -Members $NewMemberList -Confirm:$false

Lastly, if you're trying to update the membership on a ton of DLs in O365 like I needed to, then you can add the -AsJob switch to the Update-DistributionGroupMember command in your loop so that it runs all the updates in parallel rather than waiting for each membership update to finish before moving on. This will vastly speed up your script, but make sure you do some reading on PowerShell remoting and using Jobs first if you aren't familiar with them yet as there is some additional work you'll need to do afterwards to get the results, check for errors etc.

Doing it this way instead of using Add-DistributionGroupMemberreduced the number of commands sent to O365 for my DL migration from somewhere around 150K to about 2.5K and reduced my script runtime from multiple days to an evening for migrating the membership of 1200 DLs.