Importing groups/members from csv - Powershell

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 10th, 2015 6:47am

You can use https://gallery.technet.microsoft.com/Bulk-Import-Group-members-a5cbd3c8 if you change input format to something like:

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


I am not sure if it creates the groups but try it.
Free Windows Admin Tool Kit Click here and download it now
July 10th, 2015 8:12am

Thanks Slava, I thought of that but I have 2400 groups each containing up to hundreds of members, so it's not really practical!
July 10th, 2015 11:14am

You can use https://gallery.technet.microsoft.com/Bulk-Import-Group-members-a5cbd3c8 if you change input format to something like:

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


I am not sure if it creates the groups but try it.
Free Windows Admin Tool Kit Click here and download it now
July 10th, 2015 12:10pm

Hi,

Also post the updated link here.

July 13th, 2015 5:41am

Hi,

Few things that might help you,

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

Free Windows Admin Tool Kit Click here and download it now
July 13th, 2015 6:00am

Thanks all.

Posted in suggested forum:

https://social.technet.microsoft.com/Forums/scriptcenter/en-US/8eb9d93a-60a7-4ebb-841f-06ed765f97a2/creating-distribution-groups-and-adding-members?forum=ITCG

July 13th, 2015 11:10am

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

Other recent topics Other recent topics