Solved

powershell script to create 100 distribution list and add members

Posted on 2015-02-18
27
1,477 Views
Last Modified: 2016-10-20
hello,

i need a script to create bulk distribution list in exchange 2013 an add members.
the members can be user or other distribution list. for exemple, here a csv file:

alias,email,displayname,members
group1,group1@mail.com,@group1,user1@mail.com;user2@mail.com;user3@mail.com;group20@mail.com
group2,group2@mail.com,@group1,user10@mail.com;user20@mail.com;user30@mail.com;group200@mail.com

if owner is needed, we can use same owner for all distribution list


thanks for help
0
Comment
Question by:cawasaki
  • 13
  • 13
27 Comments
 
LVL 13

Expert Comment

by:Guy Lidbetter
Comment Utility
Hi cawasaki,

Are the members all in one cell and separated by a ";"  -  (Semi-colon)?

Regards

Guy
0
 

Author Comment

by:cawasaki
Comment Utility
hello,

yes, like my exemple
0
 
LVL 13

Expert Comment

by:Guy Lidbetter
Comment Utility
If so ... give this script a go...  when you are ready to commit - remove the -whatif statements.

#Import CSV
$CSV = Import-CSV <filename>
#Create Groups
$CSV | foreach {
	New-DistributionGroup -Name $_.Alias -Alias $_.Alias -Displayname $_.Displayname -PrimarySMTPAddress $_.Email -whatif
		}
#Add Members
$CSV | foreach {
	$Group = $_.Alias
	$Members = $_.members -split ";"
	ForEach ($Member in $Members) {
		 Add-DistributionGroupMember -Identity $Group -Member $Member -WhatIf 		
		}
	}
 

Open in new window

0
 
LVL 13

Expert Comment

by:Guy Lidbetter
Comment Utility
I would suggest testing on a small list of maybe 2 or three groups first.

Regards

Guy
0
 

Author Comment

by:cawasaki
Comment Utility
ok
0
 

Author Comment

by:cawasaki
Comment Utility
error:

The operation couldn't be performed because object 'group1' couldn't be found on 'domaincontroller'.
    + CategoryInfo          : NotSpecified: (:) [Add-DistributionGroupMember], ManagementObjectNotFoundException
    + FullyQualifiedErrorId : [Server=exchangeserver001,RequestId=fc0099fe-4acc-4c58-b303-08018e330085,TimeStamp=18/02/2015 11:06:04] [FailureC
   ategory=Cmdlet-ManagementObjectNotFoundException] 48F643FA,Microsoft.Exchange.Management.RecipientTasks.AddDistributionGroupMember
    + PSComputerName        : exchangeserver001.domain.com

Open in new window


and i have missed i need all the list to be created on specific Organizationnel unit

thanks for help
0
 
LVL 13

Expert Comment

by:Guy Lidbetter
Comment Utility
Did the group successfully create, and did you remove the -whatif from the end? otherwise it just tells you what it will do and doesn't actually do anything...

Updated Script  - just put in the full path to the required OU in the DL creation

$CSV = Import-CSV <file>
#Create Groups
$CSV | foreach {
	New-DistributionGroup -Name $_.Alias -Alias $_.Alias -Displayname $_.Displayname -PrimarySMTPAddress $_.Email -OrganizationalUnit "domain.com/users/folder/distribution/etc" -whatif
		}
#Add Members
$CSV | foreach {
	$Group = $_.Displayname
	$Members = $_.members -split ";"
	ForEach ($Member in $Members) {
		 Add-DistributionGroupMember -Identity $Group -Member $Member -WhatIf 		
		}
	}

Open in new window

0
 

Author Comment

by:cawasaki
Comment Utility
hello

its work perfectly, just an option is missing:

on membership approval, its created with option: anyone can leave this group without being approved by the group owner.

i need to change it to: colsed: members can be reboved only by the group owners. all requests to leave will be rejected automaticly.

possible?

thanks
0
 
LVL 13

Expert Comment

by:Guy Lidbetter
Comment Utility
Done...

$CSV = Import-CSV <file>
#Create Groups
$CSV | foreach {
	New-DistributionGroup -Name $_.Alias -Alias $_.Alias -Displayname $_.Displayname -PrimarySMTPAddress $_.Email -OrganizationalUnit "domain.com/users/folder/distribution/etc" -MemberDepartRestriction Closed -whatif
		}
#Add Members
$CSV | foreach {
	$Group = $_.Displayname
	$Members = $_.members -split ";"
	ForEach ($Member in $Members) {
		 Add-DistributionGroupMember -Identity $Group -Member $Member -WhatIf 		
		}
	}

Open in new window

0
 

Author Comment

by:cawasaki
Comment Utility
hello,

work perfectly,

its possible to get script to check if distribution list exist or not? because my csv file can contain existing list
0
 
LVL 13

Expert Comment

by:Guy Lidbetter
Comment Utility
Hi Cawasaki,

Slightly tweaked version with some out put for ya...

$CSV = Import-CSV <file>
#Create Groups
$CSV | foreach {
	$Name = $_.Alias
	$Alias = $_.Alias
	$Displayname = $_.DisplayName
	$Email = $_.email
	$Exists = Get-DistributionGroup $Name -ErrorAction SilentlyContinue
	If ($Exists) {
		New-DistributionGroup -Name $Alias -Alias $Alias -Displayname $Displayname -PrimarySMTPAddress $Email -OrganizationalUnit "domain.com/users/folder/distribution/etc" -MemberDepartRestriction Closed -whatif
		Write-Host "Distribution Group $Alias has been created" -ForegroundColor Green
		}
	ELSE {
		Write-Host "Distribution Group $Alias Already Exists" -ForegroundColor Yellow
		}
	}
#Add Members
$CSV | foreach {
	$Group = $_.Displayname
	$Members = $_.members -split ";"
	ForEach ($Member in $Members) {
		 Add-DistributionGroupMember -Identity $Group -Member $Member -WhatIf 		
		}
	}

Open in new window

0
 

Author Comment

by:cawasaki
Comment Utility
hi,

i test script with non existing distribution list, and have this error and group not created:

Distribution Group group1 Already Exists
Distribution Group group2 Already Exists
The operation couldn't be performed because object '@group1' couldn't be found on 'domaincontroller'.
    + CategoryInfo          : NotSpecified: (:) [Add-DistributionGroupMember], ManagementObjectNotFoundException
    + FullyQualifiedErrorId : [Server=exchangeserver,RequestId=438c58f8-b845-471a-b98b-361692149e90,TimeStamp=18/02/2015 14:24:40] [Fail
   ategory=Cmdlet-ManagementObjectNotFoundException] 25D8F42,Microsoft.Exchange.Management.RecipientTasks.AddDistributionGroupMembe
    + PSComputerName        : exchangeserver@domain.com

Open in new window

0
 
LVL 13

Expert Comment

by:Guy Lidbetter
Comment Utility
Check the displayname of the existing group1 is @group1
0
Wish Marketing would stop bothering you?

Is your marketing department constantly asking for new email signature updates? Are they requesting a different design for every department? Do they need yet another banner added? Don’t let it get you down! There is an easy way to manage all of these requests...

 

Author Comment

by:cawasaki
Comment Utility
yes this is what i have on my csv file
0
 
LVL 13

Expert Comment

by:Guy Lidbetter
Comment Utility
Check that the actual group that already exists in AD has the @group1 Displayname..

i.e. get-distributiongroup group1 | select displayname and make sure its @group1

Also try

get-distributiongroup @group1 and see if anything comes back or if you error
0
 

Author Comment

by:cawasaki
Comment Utility
hi;

i am sure i have delete this group, i think the problem is in the new version of script
0
 
LVL 13

Expert Comment

by:Guy Lidbetter
Comment Utility
I never changed the add-membership part which is failing. So it must be the AD object that's the issue.

Check AD replication has completed then try again,.

I'll test it my end quickly
0
 

Author Comment

by:cawasaki
Comment Utility
same issue with new group never created before:

[PS] C:\sources\script\liste>.\createlist.ps1
Distribution Group group10 Already Exists
Distribution Group group20 Already Exists
0
 
LVL 13

Expert Comment

by:Guy Lidbetter
Comment Utility
I can't believe I did that.... I had the IF statement back to front... fixed

$CSV = Import-CSV <file>
#Create Groups
$CSV | foreach {
	$Name = $_.Alias
	$Alias = $_.Alias
	$Displayname = $_.DisplayName
	$Email = $_.email
	$Exists = ""
	$Exists = Get-DistributionGroup $Name -ErrorAction SilentlyContinue
	If ($Exists) {
		Write-Host "Distribution Group $Alias Already Exists" -ForegroundColor Yellow
		}
	ELSE {
		New-DistributionGroup -Name $Alias -Alias $Alias -Displayname $Displayname -PrimarySMTPAddress $Email -OrganizationalUnit "domain.com/users/folder/distribution/etc" -MemberDepartRestriction Closed -whatif
		Write-Host "Distribution Group $Alias has been created" -ForegroundColor Green
		}
	}
#Add Members
$CSV | foreach {
	$Group = $_.Displayname
	$Members = $_.members -split ";"
	ForEach ($Member in $Members) {
		 Add-DistributionGroupMember -Identity $Group -Member $Member -WhatIf 		
		}
	}

Open in new window

0
 

Author Comment

by:cawasaki
Comment Utility
hello

work :)

and the last one think (sorry)

certain distribution list not contain memeber on csv file, sot the script create the group but with error:

Cannot process argument transformation on parameter 'Member'. Cannot convert value "" to type "Microsoft.Exchange.Configuration.Tasks.Re
cipientWithAdUserGroupIdParameter`1[Microsoft.Exchange.Configuration.Tasks.RecipientIdParameter]". Error: "Parameter values of type Micr
osoft.Exchange.Configuration.Tasks.RecipientWithAdUserGroupIdParameter`1[Microsoft.Exchange.Configuration.Tasks.RecipientIdParameter]
can't be empty. Specify a value, and try again.
Parameter name: identity"
    + CategoryInfo          : InvalidData: (:) [Add-DistributionGroupMember], ParameterBindin...mationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Add-DistributionGroupMember
    + PSComputerName        : exchangeserver01.domain.com

Open in new window


and if possible get log file to be sure all group are created?

and thank for help :)
0
 

Author Comment

by:cawasaki
Comment Utility
I've requested that this question be closed as follows:

Accepted answer: 0 points for cawasaki's comment #a40616934

for the following reason:

thanks for help
0
 
LVL 13

Expert Comment

by:Guy Lidbetter
Comment Utility
Please accept one of my scripts as the answer not your own comments. thank you.
0
 
LVL 13

Accepted Solution

by:
Guy Lidbetter earned 500 total points
Comment Utility
Updated script to avoid error and print out error logs

$Error.Clear()
#Create Bulkk Distribution groups from CV with members in once cell separated by ;
$CSV = Import-CSV <file>
#Create Groups
$CSV | foreach {
	$Name = $_.Alias
	$Alias = $_.Alias
	$Displayname = $_.DisplayName
	$Email = $_.email
	$Exists = ""
	$Exists = Get-DistributionGroup $Name -ErrorAction SilentlyContinue
	If ($Exists) {
		Write-Host "Distribution Group $Alias Already Exists" -ForegroundColor Yellow
		}
	ELSE {
		New-DistributionGroup -Name $Alias -Alias $Alias -Displayname $Displayname -PrimarySMTPAddress $Email -OrganizationalUnit "domain.com/users/folder/distribution/etc" -MemberDepartRestriction Closed -ErrorAction SilentlyContinue -whatif
		Write-Host "Distribution Group $Alias has been created" -ForegroundColor Green
		}
	}
#Add Members
$CSV | foreach {
	$Group = $_.Displayname
	$Members = $_.members -split ";"
	IF ($Members) { 
		ForEach ($Member in $Members) {
		 Add-DistributionGroupMember -Identity $Group -Member $Member -WhatIf 		
		}
	}}
 $Error | Out-File c:\ErrorLog.txt

Open in new window

0
 

Author Comment

by:cawasaki
Comment Utility
oh sorry !
0
 
LVL 13

Expert Comment

by:Guy Lidbetter
Comment Utility
No probs, did you get the updated script?
0
 

Author Comment

by:cawasaki
Comment Utility
yes thanks :)
0
 

Expert Comment

by:Chris Aument
Comment Utility
I found simpler way to do this using PS 5.0. You only need the Name, Alias, and Members columns in your CSV file.

# Specify CSV location
$path = "C:\temp\DGroup.csv"

# Prompt for 365 credentials
$cred = Get-Credential

# Specify 365 session parameters
$s = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $cred -Authentication Basic -AllowRedirection

# Connect to Exchange Online and begin session
Import-PSSession $s -AllowClobber
Connect-MsolService -Credential $cred

# Create Distribution Group and add members
Import-CSV $path | foreach {
New-DistributionGroup -Name $_.Name -Alias $_.Alias -PrimarySmtpAddress "$($_.Alias)@contoso.org" -Members ($_.Members -Split ";") -MemberDepartRestriction Closed -MemberJoinRestriction Closed
}

# End session
Remove-PSSession $s
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Disabling the Directory Sync Service Account in Office 365 will stop directory synchronization from working.
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.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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

8 Experts available now in Live!

Get 1:1 Help Now