Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5449
  • Last Modified:

powershell script to create 100 distribution list and add members

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
cawasaki
Asked:
cawasaki
  • 13
  • 13
1 Solution
 
Guy LidbetterCommented:
Hi cawasaki,

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

Regards

Guy
0
 
cawasakiAuthor Commented:
hello,

yes, like my exemple
0
 
Guy LidbetterCommented:
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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
Guy LidbetterCommented:
I would suggest testing on a small list of maybe 2 or three groups first.

Regards

Guy
0
 
cawasakiAuthor Commented:
ok
0
 
cawasakiAuthor Commented:
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
 
Guy LidbetterCommented:
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
 
cawasakiAuthor Commented:
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
 
Guy LidbetterCommented:
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
 
cawasakiAuthor Commented:
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
 
Guy LidbetterCommented:
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
 
cawasakiAuthor Commented:
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
 
Guy LidbetterCommented:
Check the displayname of the existing group1 is @group1
0
 
cawasakiAuthor Commented:
yes this is what i have on my csv file
0
 
Guy LidbetterCommented:
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
 
cawasakiAuthor Commented:
hi;

i am sure i have delete this group, i think the problem is in the new version of script
0
 
Guy LidbetterCommented:
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
 
cawasakiAuthor Commented:
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
 
Guy LidbetterCommented:
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
 
cawasakiAuthor Commented:
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
 
cawasakiAuthor Commented:
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
 
Guy LidbetterCommented:
Please accept one of my scripts as the answer not your own comments. thank you.
0
 
Guy LidbetterCommented:
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
 
cawasakiAuthor Commented:
oh sorry !
0
 
Guy LidbetterCommented:
No probs, did you get the updated script?
0
 
cawasakiAuthor Commented:
yes thanks :)
0
 
Chris AumentCommented:
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 13
  • 13
Tackle projects and never again get stuck behind a technical roadblock.
Join Now