Creating Distribution Groups and adding members

Folks, I'm trying to import a MASSIVE list of groups and their members from a csv file into AD.

The csv is in this format:

group,members
group1,"user1,user2,user3"
group2,"user4,user5,user6"

The code I started with was (in 2 stages):

1. Create the groups

$file = import-csv c:\test1.csv ; foreach ($line in $file) {New-DistributionGroup -name $line.DLName -organizationalunit 'OU=GW DLs,DC=company,DC=com' ; set-distributiongroup $line.DLName  -customAttribute9 "GW"}

2. Add the users:

foreach ($line in $file) {$line.members -split ',' | %{get-recipient $_ | Add-DistributionGroupMember $line.DLName}}

This would work if the users didn't have duplicates all over the place (most have a user AND a contact...), but it craps out because of the duplicates (e.g. user1 is the alias for a mbx and contact). So I split them up into:

foreach ($line in $file) {$line.members -split ',' | %{get-mailbox $_ | Add-DistributionGroupMember $line.DLName

And did the same for each type (get-mailuser, get-distributiongroup etc).

This took over 2 days to complete,probably because I'm doing client-side filtering or something) and it's still not great - I think some of the nested group memberships didn't work properly. (I looked at Add-AdGroupMember but this apparently crashes out if the object is already a member.)

Can anyone give me a better way of doing this that will work reliably, and quicker! I'd like to add ALL of the duplicates, not just the mbxs for example.

Many thanks,

F.

July 13th, 2015 11:10am

I've found it much easier to do this with two import files.  One is the list of groups and the associated properties of the group, such as the e-mail address and display name.  The other list associates a group with one member.  After creating the empty groups in the first script, your script can run through the members and add each member to its group.
Free Windows Admin Tool Kit Click here and download it now
July 13th, 2015 2:21pm

Hi,

Posting it back from earlier post.

Few things that might help you,

"I looked at Add-AdGroupMember but this apparently crashes out if the object is already a member.)"

July 14th, 2015 2:50am

Thanks Ed, I considered something similar but the format I have it in would mean a lot of work modifying the csv file.

There's no method that'll take less time than I have to do it, that's clear. :-)

Free Windows Admin Tool Kit Click here and download it now
July 16th, 2015 11:55am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics