Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create distribution group via Powershell

Status
Not open for further replies.

disturbedone

Vendor
Sep 28, 2006
781
AU
I'm looking at creating Distribution Groups from a CSV. This is for student classes.

The commands:
New-DistributionGroup groupnamegoeshere
Add-DistributionGroupMember -Identity "groupnamegoeshere" -Member usernamegoeshere}

look straight forward but I'd like to tie it all into a nice Powershell script.

The CSV would look something like:
classname,username
AA1234,2012SMITHJ
AA1234,2012DOEJ
ZZ9876,2013JOHNSA
ZZ9876,2013BLAHB

I'd like to create Distribution Groups called eg 'CLASS AA1234' and put the relevant members in.

I've seen a command:
Import-Csv .\StudentClasses.csv | ForEach {Add-DistributionGroupMember -Identity "AA1234" -Member $_.username}

but that doesn't allow the adding of 'CLASS ' in front of the class name.

I'm relatively new to scripting. I've used scripts that ask for a user's input eg 'What is the class name?' and take what was entered eg 'AA1234' and put the 2 variables together into a single variable but am unsure how that could be achieved from a CSV.

Any assistance is greatly appreciated.
 
So - without testing this, something like this should be close:
Code:
$Class = Read-Host "Enter class"
$Students = Import-Csv .\StudentClasses.csv | ? {$_.ClassName -eq $class}
New-DistributionGroup $Class
ForEach ($Student in $Students){
Add-DistributionGroupMember -Identity "AA1234" -Member $Student.UserName}

That would give you a script that will prompt for a class name, then create the DL and add the members for that particular class.

Do you have your Tek-Tips.com Swag? I've got mine!

Stop by the new Tek-Tips group at LinkedIn.
 
Whoops - let's try this
Code:
$Class = Read-Host "Enter class"
$Students = Import-Csv .\StudentClasses.csv | ? {$_.ClassName -eq $class}
New-DistributionGroup $Class
ForEach ($Student in $Students){
Add-DistributionGroupMember -Identity $Class -Member $Student.UserName}



Do you have your Tek-Tips.com Swag? I've got mine!

Stop by the new Tek-Tips group at LinkedIn.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top