Solved

Create Bulk Distribution Groups with Various Members using Powershell 2.0

Posted on 2014-03-07
7
494 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:kt KTAdmin
  • 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:kt KTAdmin
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:kt KTAdmin
ID: 39920819
Yes, the DG's are already created.
0
 

Author Closing Comment

by:kt KTAdmin
ID: 39923299
This took care of my needs.

Thank you!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
Following basic email etiquette rules will help you write a professional email and achieve a good, lasting impression with your contacts.
In this video we show how to create a mailbox database in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Servers >> Data…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now