Migrate Distribution Groups and Members between 365 Tenants and settings

Alex has already done the hard work for this which works well

Exchange Online Distribution List Migration Script | A blog about automation and technologies in the cloud (alexholmeset.blog)

I changed the script as the Source tenant had multiple domains and also stoped 365 Groups with the same name causing issues

#Connect with source tenant
Connect-AzureAD

#Connect with destination tenant
Connect-ExchangeOnline


#Dist Groups list in array "1","2"
$DL = "1","2"


$ZSourceDomain = "sourcedomain.com"
$CSourceDomain = "sourcedomain2.com"
$DestinationDomain = "destinationdomain3.com"

foreach($D in $DL){

$List = @()
$List = Get-AzureADGroup -SearchString "$D"
#Remove office 365 Groups with same name , using SPO as identifier
$List = $list | ?{$_.proxyaddresses -match '.*SPO.*'}

$ZListMembers = @()
$ZListMembers = Get-AzureADGroupMember -ObjectId $List.ObjectId | Where-Object {$_.UserPrincipalName -like '*$ZSourceDomain'}

$ZListMembersUPN = @()
$ZListMembersUPN = $ZListMembers.UserPrincipalName

$CListMembers = @()
$CListMembers = Get-AzureADGroupMember -ObjectId $List.ObjectId | Where-Object {$_.UserPrincipalName -like '*$CSourceDomain'}

$CListMembersUPN = @()
$CListMembersUPN = $CListMembers.UserPrincipalName


New-DistributionGroup -Name $List.DisplayName -Description $List.Description -PrimarySmtpAddress $($List.MailNickName+"@$DestinationDomain")

foreach($ZListMemberUPN in $ZListMembersUPN){


Add-DistributionGroupMember -Identity $List.DisplayName -Member $ZListMemberUPN.replace("$ZSourceDomain","$DestinationDomain")

}

foreach($CListMemberUPN in $CListMembersUPN){


Add-DistributionGroupMember -Identity $List.DisplayName -Member $CListMemberUPN.replace("$CSourceDomain","$DestinationDomain")

}




}

Lets Export all Distribution settings from Source Tenant

#Connect with source tenant
Connect-ExchangeOnline

Get-DistributionGroup -RecipientTypeDetails MailUniversalDistributionGroup | Export-Csv -Path “C:\Temp\dGroupsall.csv"

Lets Import and Set what we need to the Destination Tenant
This script does RequireSenderAuthenticationEnabled ( Delivery Restrictions ) , Owner ( ManagedBy ) and MemberJoinRestriction. You can add any new ones you would like to set from the Sheet

#Connect with destination tenant
Connect-ExchangeOnline

Import-Csv -Path "C:\Temp\dGroupsall.csv" | ForEach-Object {

#Lets do this one by one incase one fails it doesn’t block other

#We need to convert the csv value to boolean to write to RequireSenderAuthenticationEnabled
[boolean] $RequireSenderAuthenticationEnabled = [system.convert]::toboolean($_.RequireSenderAuthenticationEnabled)
Set-DistributionGroup -Identity $_.Alias -RequireSenderAuthenticationEnabled $RequireSenderAuthenticationEnabled
Set-DistributionGroup -Identity $_.Alias -ManagedBy $_.ManagedBy
Set-DistributionGroup -Identity $_.Alias -MemberJoinRestriction $_.MemberJoinRestriction

}
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...