Solved

powershell script to create 100 distribution list and add members

Posted on 2015-02-18
27
3,098 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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
 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Lotus Notes – formerly IBM Notes – is an email client application, while IBM Domino (earlier Lotus Domino) is an email server. The client possesses a set of features that are even more advanced as compared to that of Outlook. Likewise, IBM Domino is…
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

749 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