Skip to content

Instantly share code, notes, and snippets.

@AlexanderHolmeset
Created January 6, 2022 11:38
Show Gist options
  • Save AlexanderHolmeset/123730e4ffed1fe39cecf95264a93086 to your computer and use it in GitHub Desktop.
Save AlexanderHolmeset/123730e4ffed1fe39cecf95264a93086 to your computer and use it in GitHub Desktop.
#Connect with source tenant
Connect-AzureAD
#Connect with destination tenant
Connect-ExchangeOnline
$DL = "distrobutionlist1","distrobutionlist2"
$SourceDomain = "alexholmeset.blog"
$DestinationDomain = "contoso.com"
foreach($D in $DL){
$List = @()
$List = Get-AzureADGroup -SearchString "$D"
$ListMembers = @()
$ListMembers = Get-AzureADGroupMember -ObjectId $List.ObjectId
$ListMembersUPN = @()
$ListMembersUPN = $ListMembers.UserPrincipalName
New-DistributionGroup -Name $List.DisplayName -Description $List.Description -PrimarySmtpAddress $($List.MailNickName+"@$DestinationDomain")
foreach($ListMemberUPN in $ListMembersUPN){
Add-DistributionGroupMember -Identity $List.DisplayName -Member $ListMemberUPN.replace("$SourceDomain","$DestinationDomain")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment