cawasaki
asked on
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,me mbers
group1,group1@mail.com,@gr oup1,user1 @mail.com; user2@mail .com;user3 @mail.com; group20@ma il.com
group2,group2@mail.com,@gr oup1,user1 0@mail.com ;user20@ma il.com;use r30@mail.c om;group20 0@mail.com
if owner is needed, we can use same owner for all distribution list
thanks for help
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,me
group1,group1@mail.com,@gr
group2,group2@mail.com,@gr
if owner is needed, we can use same owner for all distribution list
thanks for help
ASKER
hello,
yes, like my exemple
yes, like my exemple
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
}
}
I would suggest testing on a small list of maybe 2 or three groups first.
Regards
Guy
Regards
Guy
ASKER
ok
ASKER
error:
and i have missed i need all the list to be created on specific Organizationnel unit
thanks for help
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
and i have missed i need all the list to be created on specific Organizationnel unit
thanks for help
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
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
}
}
ASKER
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
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
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
}
}
ASKER
hello,
work perfectly,
its possible to get script to check if distribution list exist or not? because my csv file can contain existing list
work perfectly,
its possible to get script to check if distribution list exist or not? because my csv file can contain existing list
Hi Cawasaki,
Slightly tweaked version with some out put for ya...
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
}
}
ASKER
hi,
i test script with non existing distribution list, and have this error and group not created:
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
Check the displayname of the existing group1 is @group1
ASKER
yes this is what i have on my csv file
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
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
ASKER
hi;
i am sure i have delete this group, i think the problem is in the new version of script
i am sure i have delete this group, i think the problem is in the new version of script
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
Check AD replication has completed then try again,.
I'll test it my end quickly
ASKER
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
[PS] C:\sources\script\liste>.\
Distribution Group group10 Already Exists
Distribution Group group20 Already Exists
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
}
}
ASKER
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:
and if possible get log file to be sure all group are created?
and thank for help :)
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
and if possible get log file to be sure all group are created?
and thank for help :)
ASKER
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
Accepted answer: 0 points for cawasaki's comment #a40616934
for the following reason:
thanks for help
Please accept one of my scripts as the answer not your own comments. thank you.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
oh sorry !
No probs, did you get the updated script?
ASKER
yes thanks :)
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
# 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
Are the members all in one cell and separated by a ";" - (Semi-colon)?
Regards
Guy