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:
would easily do the trick. Failing that, though, there's no cmdlet to do exactly what you want. You could do a
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:
or
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 toUpdate-DistributionGroupMember
like the following: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-DistributionGroupMember
reduced 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.Successfully populated a Distribution Group by creating a text file, populating with member account aliases, then executing the command:
See these URLs for further details:
Not sure why the teams at Microsoft made this so complicated. Sometimes you just have to think simple.
Try this, it seems to work for me:
Get-Content | Add-DistributionGroupMember
Text file just contains the aliases of the users that you want to add.
It looks like the Quest Active Directory PowerShell Cmdlets can add multiple users to a group at one time per this wiki page. In my brief testing, I was able to use the Add-QADGroupMember cmdlet to add a small array of users to a test group in Active Directory.
It might be helpful too to add AD with this:
import-module ActiveDirectory
Then just use:
Add-AdgroupMember MyList -member user.alias,user2.alias,user3.alias,...
For this you do need RSAT installed (MS product, and free) or it won't work.
If performance is a concern Add-ADGroupMember is by far the fastest, and as mentioned can take an array as input to the -Member parameter.
Something like that, not sure if this is well-written or working - just an idea.
For adding bulk users from text file use the below script, it is very easy to use and this guy has written pretty neat
https://gallery.technet.microsoft.com/Add-Bulk-Users-to-6f3014b2