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 2:50am

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

Other recent topics Other recent topics