Solved

Create Bulk Distribution Groups with Various Members using Powershell 2.0

Posted on 2014-03-07
7
508 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
  • 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

A brief introduction to what I consider to be the best editor for PowerShell.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
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…
This video discusses moving either the default database or any database to a new volume.

770 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