Solved

Create Bulk Distribution Groups with Various Members using Powershell 2.0

Posted on 2014-03-07
7
528 Views
Last Modified: 2014-03-12
I have a need to create 10 Distribution Groups with various members in them. Please assist in providing the PS script.

CSV Columns may be:

Usernames, DG names, Alias
0
Comment
Question by:K Anthony O365
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39913636
$ou = "your ou in OU=xxx,dc=xxx,dc=xx,dc=x"
$Contents = import-csv = "path"
foreach ($content in $contents)
{
New-DistributionGroup -Name $content.dgnames -OrganizationalUnit $OU -SamAccountName $content.username -Type "Security"
}

put you csv file in column and rename "DG Names" to "DGnames" no space
0
 
LVL 4

Expert Comment

by:Jason Ryberg
ID: 39913638
$Info = Import-CSV C:\pathToFile\DGs.csv

$info | % {

  New-DistributionGroup -Name $_.DGName

}

$info | % {

  Add-DistributionGroupMember -Identity $_.DGName -Member $_.username

}

Open in new window

0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39913642
is the username in all DG group?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:K Anthony O365
ID: 39920351
Here is what I have:

UserName, DGName1, DGName2, DGname3
user1  
user2
user3
user4

User1 is part of DGName1 and DGName3
User2 is part of DGName2 and DGName3
User3 is part of DGName1 and DGName2

so I have 100 users and they are part of 10 different DG's. How would I structure the csv file?
0
 
LVL 4

Accepted Solution

by:
Jason Ryberg earned 500 total points
ID: 39920486
Are you manually creating the CSVs?  I only ask because scripts are usually based on information that already exists.  So that said, the structure of the CSV would depend on which set of data would require less typing.  Without seeing a representative dataset, it's hard to recommend which way to go.  Assuming each user is in a DG, I would create the CSV like this (see attached file for actual test data):

UserName Membership
user1          DG1,DG2,DG10
user2          DG2,DG3,DG7
user3          DG8,DG1,DG2

It would be preferable to have created the DGs beforehand (thus eliminating the need to check for DG creation within the loop):

$DGs = Import-CSV C:\pathToCSV\DGs.csv

$DGs | % {

  New-DistributionGroup -Name $_.DGName

}

Open in new window


I would then nest a loop for the entire file to add membership to each user:

$users = Import-CSV C:\pathToFile\Users.csv

$users | % {

  $username = $_.Username
  $membership = $_.Membership.split(",")

  $membership | % {

    Add-DistributionGroupMember -Identity $_ -Member $Username

  }

}

Open in new window

test.csv
0
 

Author Comment

by:K Anthony O365
ID: 39920819
Yes, the DG's are already created.
0
 

Author Closing Comment

by:K Anthony O365
ID: 39923299
This took care of my needs.

Thank you!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Check out this step-by-step guide for using the newly updated Experts Exchange mobile app—released on May 30.
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
how to add IIS SMTP to handle application/Scanner relays into office 365.

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question