?
Solved

Create Bulk Distribution Groups with Various Members using Powershell 2.0

Posted on 2014-03-07
7
Medium Priority
?
539 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:Anthony K 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

Author Comment

by:Anthony K 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 2000 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:Anthony K O365
ID: 39920819
Yes, the DG's are already created.
0
 

Author Closing Comment

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

Thank you!
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

A couple of months ago we ran into an issue that necessitated re-creating our Edge Subscriptions. However, when we attempted to execute the command: New-EdgeSubscription -filename C:\NewEdgeSub_01.xml we received an error indicating that the LDAP se…
New style of hardware planning for Microsoft Exchange server.
This video discusses moving either the default database or any database to a new volume.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Suggested Courses

752 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