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 12:10pm

Hi Fergus,

I recommend you post this in Script forum also, as they should have more professional knowledge on scripting and you may get effective solution timely.

Best regards,

July 12th, 2015 9:49pm

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

Other recent topics Other recent topics