Solved

powershell script to create 100 distribution list and add members

Posted on 2015-02-18
27
1,939 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
ID: 40616177
Hi cawasaki,

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

Regards

Guy
0
 

Author Comment

by:cawasaki
ID: 40616195
hello,

yes, like my exemple
0
 
LVL 13

Expert Comment

by:Guy Lidbetter
ID: 40616198
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
ID: 40616206
I would suggest testing on a small list of maybe 2 or three groups first.

Regards

Guy
0
 

Author Comment

by:cawasaki
ID: 40616214
ok
0
 

Author Comment

by:cawasaki
ID: 40616217
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
ID: 40616228
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
ID: 40616339
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
ID: 40616350
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
ID: 40616517
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
ID: 40616550
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
ID: 40616588
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
ID: 40616597
Check the displayname of the existing group1 is @group1
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 

Author Comment

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

Expert Comment

by:Guy Lidbetter
ID: 40616615
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
ID: 40616629
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
ID: 40616644
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
ID: 40616648
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
ID: 40616673
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
ID: 40616934
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
ID: 40618742
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
ID: 40618743
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
ID: 40618757
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
ID: 40618778
oh sorry !
0
 
LVL 13

Expert Comment

by:Guy Lidbetter
ID: 40618782
No probs, did you get the updated script?
0
 

Author Comment

by:cawasaki
ID: 40618860
yes thanks :)
0
 

Expert Comment

by:Chris Aument
ID: 41852222
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

This article will help you understand what HashTables are and how to use them in PowerShell.
Learn to move / copy / export exchange contacts to iPhone without using any software. Also see the issues in configuration of exchange with iPhone to migrate contacts.
To show how to generate a certificate request 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 >> Certificates‚Ķ
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

863 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

21 Experts available now in Live!

Get 1:1 Help Now