Migrating on prem disribution group to exchange online - Hybrid

%3CLINGO-SUB%20id%3D%22lingo-sub-1228902%22%20slang%3D%22en-US%22%3EMigrating%20on%20prem%20disribution%20group%20to%20exchange%20online%20-%20Hybrid%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1228902%22%20slang%3D%22en-US%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E*%20Hybrid%20setup%20(Exchange%202013%20-%20Single%20Server)%3C%2FP%3E%3CP%3E*%20All%20users%20already%20migrated%20to%20EXO%3C%2FP%3E%3CP%3E*%20its%20time%20to%20migrate%20distribution%20groups%20(Almost%20200)%20-%20Already%20synced%20using%20AAD%3C%2FP%3E%3CP%3E*%20At%20present%20IT%20is%20controlling%20distribution%20groups%20and%20we%20want%20the%20same%20way%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20would%20like%20to%20know%20what%20is%20the%20best%20way%20to%20migrate%20distribution%20group%20to%20EXO.%26nbsp%3B%20%26nbsp%3BIs%20there%20any%20latest%20script%20%2F%20method%26nbsp%3B%20%3F%26nbsp%3B%20I%20have%20found%20few%20years%20old%20discussion%20but%20not%20very%20clear%20to%20me.%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20in%20advance%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1228902%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1229554%22%20slang%3D%22en-US%22%3ERe%3A%20Migrating%20on%20prem%20disribution%20group%20to%20exchange%20online%20-%20Hybrid%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1229554%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F582114%22%20target%3D%22_blank%22%3E%40SASK2381%3C%2FA%3E%26nbsp%3BHi%2C%20since%20you%20have%20on-prem%20AD%20and%20are%20syncing%20using%20AD%20connect%2C%20you%20actually%20don't%20migrate%20your%20DLs.%20AD%20is%20the%20source%20of%20user%20data%20including%20users%20and%20groups%2C%20so%20you%20continue%20to%20manage%20them%20using%20AD%20users%20and%20computers%2C%20and%20Exchange%20admin%20center%20(on-prem).%20That%20includes%20mailbox%20management%2C%20creating%20mailboxes%2C%20manage%20DL%20membership%20etc.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1229714%22%20slang%3D%22en-US%22%3ERe%3A%20Migrating%20on%20prem%20disribution%20group%20to%20exchange%20online%20-%20Hybrid%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1229714%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F431307%22%20target%3D%22_blank%22%3E%40CloudHal%3C%2FA%3E%26nbsp%3B%20Thanks%20for%20response%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eyes%20that's%20true%20right%20now%20%2C%20but%20we%20want%20to%20get%20rid%20of%20on-prem%20exchange%20.%26nbsp%3B%20Do%20you%20think%20it%20will%20work%20fine%20when%20we%20turn%20off%20on-prem%20exchange%20server%20%3F%26nbsp%3B%20and%20we%20just%20able%20to%20manage%20them%20from%20AD%20%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1229774%22%20slang%3D%22en-US%22%3ERe%3A%20Migrating%20on%20prem%20disribution%20group%20to%20exchange%20online%20-%20Hybrid%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1229774%22%20slang%3D%22en-US%22%3EWhat%20you%20are%20suggesting%20is%20possible%20but%20unsupported.%20You%20can%20manage%20email%20attributes%20using%20AD%20users%20and%20computers%2C%20but%20it%20is%20not%20so%20easy%20for%20support%2C%20and%20user%20accounts%20won%E2%80%99t%20have%20all%20the%20correct%20email%20attributes.%20For%20example%20managing%20proxy%20addresses%20is%20prone%20to%20error.%3CBR%20%2F%3EThe%20only%20way%20of%20retaining%20AD%20at%20the%20moment%20is%20to%20also%20retain%20exchange%20for%20management.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1229948%22%20slang%3D%22en-US%22%3ERe%3A%20Migrating%20on%20prem%20disribution%20group%20to%20exchange%20online%20-%20Hybrid%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1229948%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F431307%22%20target%3D%22_blank%22%3E%40CloudHal%3C%2FA%3E%26nbsp%3B%20thanks%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eso%20if%20I%20want%20to%20get%20rid%20of%20on-prem%20exchange%20%2C%20the%20only%20option%20is%20to%20recreate%20them%20all%20in%20EXO%2C%26nbsp%3B%20in%20that%20case%20what%20is%20the%20best%20method%2C%20script%2C%20best%20practice%20to%20follow%20%3F%26nbsp%3B%3C%2FP%3E%3CP%3EManually%20deletion%20and%20recreating%20is%20time%20consuming%20and%20also%20temporary%20disruptive%20as%20I%20have%20almost%20200%20Distributions%20group.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2215662%22%20slang%3D%22en-US%22%3ERe%3A%20Migrating%20on%20prem%20disribution%20group%20to%20exchange%20online%20-%20Hybrid%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2215662%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F582114%22%20target%3D%22_blank%22%3E%40SASK2381%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%23%23%23%23%23%23%23%23%20Global%20Variables%20%23%23%23%23%23%23%23%23%23%23%23%23%0ASet-Location%20-Path%20%24env%3AUSERPROFILE%5CDocuments%0A%23Get-PSSession%20%7C%20Remove-PSSession%0A%24UserCredential%20%3D%20Get-Credential%0A%24ListOfGroupsToMigrate%20%3D%20Import-csv%20.%5CGroupMigration.csv%0A%23%24SingleGroupToMigratePSMTP%20%3D%20'!BOMBSHELLtheatricalteams%40lionsgate.com'%0A%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%0A%24ListOfGroupsToMigrate%20%7C%20ForEach-Object%20%7B%0A%24SingleGroupToMigratePSMTP%20%3D%20%24_.GroupEmailAddress%0A%24Session%20%3D%20New-PSSession%20-ConfigurationName%20Microsoft.Exchange%20-ConnectionUri%20http%3A%2F%2FEXCHSVRFQDN.com%2FPowerShell%2F%20-Authentication%20Kerberos%20-Credential%20%24UserCredential%0AImport-PSSession%20%24Session%20-Allowclobber%0A%24distrotomovedetails%20%3D%20Get-DistributionGroup%20-Identity%20%24SingleGroupToMigratePSMTP%20%0A%24distrotomovedetails%20%7C%20Export-csv%20.%5Csinglegroupdetailsbeforedelete.csv%20-NoTypeInformation%0A%24distrotomovedetails%20%7C%20ForEach-Object%7B%0A%20%20%20%20%24DistributionGroupName%20%3D%20%24_.PrimarySmtpAddress%0A%20%20%20%20Get-DistributionGroupMember%20-Identity%20%24DistributionGroupName%20%7C%20ForEach-Object%7B%0A%20%20%20%20%20%20%20%20%5BPSCustomObject%5D%40%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20DistributionGroup%20%3D%20%24DistributionGroupName%0A%20%20%20%20%20%20%20%20%20%20%20%20MemberName%20%3D%20%24_.DisplayName%0A%20%20%20%20%20%20%20%20%20%20%20%20EmailAddress%20%3D%20%24_.PrimarySmtpAddress%0A%20%20%20%20%20%20%20%20%20%20%20%23Other%20recipient%20properties%20here%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%20%7C%20Export-csv%20.%5CGroupmembersBeforeDel.csv%20-NoTypeInformation%0A%0A%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%0A%24groupowner%20%3D%20%24distrotomovedetails.ManagedBy%0A%24GroupOwnerCorrected%20%3D%20%24groupowner.split('%2F')%5B-1%5D%0A%24GroupOwnerCorrected.ToString()%0A%24Managedby%20%3D%20Get-ADuser%20-Filter%20%22displayName%20-eq%20'%24GroupOwnerCorrected'%22%0A%24distrotomovedetails%20%7C%20fl%0A%0A%23%20Extract%20All%20Email%20Addresses%20for%20single%20group%20one%20per%20line%20so%20you%20can%20easily%20add%20back%20in%20EXO%0A%24addresses%20%3D%20%40()%0A%20%20%20%20Foreach%20(%24mbx%20in%20%24distrotomovedetails)%20%7B%0A%20%20%20%20Foreach%20(%24address%20in%20%24mbx.EmailAddresses)%20%7B%0A%20%20%20%20%24obj%20%3D%20%22%22%20%7C%20Select-Object%20Identity%2CAlias%2CEmailAddress%2CRecipientType%0A%20%20%20%20%24obj.Alias%20%3D%20%24mbx.Alias%0A%20%20%20%20%24obj.Identity%20%3D%20%24mbx.Identity%0A%20%20%20%20%24obj.RecipientType%20%3D%20%24mbx.RecipientType%0A%20%20%20%20%24obj.EmailAddress%20%3D%20%24address.ToString().SubString(0)%0A%20%20%20%20%24addresses%20%2B%3D%20%24obj%20%0A%20%20%20%20%20%20%20%20%7D%20%7D%20%24addresses%20%7C%20Export-Csv%20.%5CSingleDistroGroupsmtpaddresses.csv%20-NoTypeInformation%0A%24addresses%0A%23Remove%20OnPremises%20Group%0ARemove-DistributionGroup%20-Identity%20%24distrotomovedetails.Identity%20-Confirm%3A%24false%20-Verbose%0AStart-Sleep%20120%0A%0A%23Create%20On-Premises%20Contact%0A%24ExternalEmailAddress%20%3D%20%24distrotomovedetails.Alias%20%2B%20%22%40TENANT.mail.onmicrosoft.com%22%0ANew-MailContact%20-DisplayName%20%24distrotomovedetails.DisplayName%20-Alias%20%24distrotomovedetails.Alias%20-ExternalEmailAddress%20%24ExternalEmailAddress%20-OrganizationalUnit%20%22OU%3DContacts%2COU%3DExchange%20Accounts%2CDC%3Dlge-mdr%2CDC%3Dcom%22%0ASet-MailContact%20-Identity%20%24distrotomovedetails.PrimarySmtpAddress%20-HiddenFromAddressListsEnabled%20%24true%0A%0A%23Close%20Session%20with%20OnPremise%20Exchange%0AGet-PSSession%20%7C%20Remove-PSSession%20-Verbose%0A%0A%23Connect%20EXO%0AImport-Module%20ExchangeOnlineManagement%0AConnect-ExchangeOnline%20-Credential%20%24UserCredential%0AWrite-Host%20-ForgroundColor%20Cyan%20%22Running%20AAD%20Connect%20Sync%22%0A%23Remotely%20Run%20AAD%20Connect%20Delta%20Sync%0A%24session%20%3D%20New-PSSession%20-ComputerName%20AADConnectSVRFQDN.com%0AInvoke-Command%20-Session%20%24session%20-ScriptBlock%20%7BImport-Module%20-Name%20'ADSync'%7D%0AInvoke-Command%20-Session%20%24session%20-ScriptBlock%20%7BStart-ADSyncSyncCycle%20-PolicyType%20Delta%7D%0ARemove-PSSession%20%24session%0A%23Pause%20until%20AAD%20Connect%20Sync%20can%20run%20and%20Azure%20AD%20to%20catch%20up%0AWrite-Host%20-ForgroundColor%20Green%20%22Pausing%20for%20AAD%20Connect%20to%20run%20and%20then%20we%20check%20EXO%20for%20the%20group%22%0Afunction%20Start-Sleep(%24seconds)%20%7B%0A%20%20%20%20%24doneDT%20%3D%20(Get-Date).AddSeconds(%24seconds)%0A%20%20%20%20while(%24doneDT%20-gt%20(Get-Date))%20%7B%0A%20%20%20%20%20%20%20%20%24secondsLeft%20%3D%20%24doneDT.Subtract((Get-Date)).TotalSeconds%0A%20%20%20%20%20%20%20%20%24percent%20%3D%20(%24seconds%20-%20%24secondsLeft)%20%2F%20%24seconds%20*%20100%0A%20%20%20%20%20%20%20%20Write-Progress%20-Activity%20%22Sleeping%22%20-Status%20%22Sleeping...%22%20-SecondsRemaining%20%24secondsLeft%20-PercentComplete%20%24percent%0A%20%20%20%20%20%20%20%20%5BSystem.Threading.Thread%5D%3A%3ASleep(500)%0A%20%20%20%20%7D%0A%20%20%20%20Write-Progress%20-Activity%20%22Sleeping%22%20-Status%20%22Sleeping...%22%20-SecondsRemaining%200%20-Completed%0A%7D%0A%0AStart-Sleep%20200%0AWrite-host%20-ForegroundColor%20Cyan%20%22Verifing%20the%20group%20%20is%20gone%20from%20Azure%20AD%2C%20command%20will%20proceed%20once%20group%20is%20not%20showing%20in%20EXO%22%20%24distrotomovedetails.PrimarySmtpAddress%0Awhile(%24GetDistroFinder%20-ne%20%24null)%7B%0AWrite-Host%20-ForegroundColor%20Cyan%20%22Checking%20EXO%20for%20the%20old%20synced%20Distro%20Group%22%0A%24GetDistroFinder%20%3D%20Get-DistributionGroup%20-Identity%20%24distrotomovedetails.PrimarySmtpAddress%0AStart-Sleep%2010%0A%7D%0AWrite-Host%20-ForegroundColor%20Green%20%22Group%20Appears%20to%20have%20been%20removed%20please%20verify%20on%20Azure%20AD%22%0ARead-Host%20-Prompt%20%22Hit%20Enter%20To%20Proceed%22%0A%0A%23%23%23%23%23%23%23%23%23%23%23%23%20Recreate%20Group%20in%20the%20cloud%20%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%0AWrite-Host%20-ForegroundColor%20Cyan%20%22Recreating%20the%20group%20in%20EXO%22%0A%23Create%20the%20EXO%20Distro%20group%0ANew-DistributionGroup%20-DisplayName%20%24distrotomovedetails.DisplayName%20-Alias%20%24distrotomovedetails.Alias%20-Name%20%24distrotomovedetails.Name%20-MemberDepartRestriction%20%24distrotomovedetails.MemberDepartRestriction%20-MemberJoinRestriction%20%24distrotomovedetails.MemberJoinRestriction%20-PrimarySmtpAddress%20%24distrotomovedetails.PrimarySmtpAddress%20-RequireSenderAuthenticationEnabled%3A%24false%20-ManagedBy%20%24Managedby.UserPrincipalName%20-Verbose%0A%0A%23Add%20additional%20SMTP%20and%20x500%20addresses%20to%20the%20group.%0AWrite-Host%20-ForegroundColor%20Green%20%22Adding%20SMTP%20and%20x500%20Addresses%20Back%20to%22%20%24distroprimarysmtp%0A%24EmailAddressesToAdd%20%3D%20Import-Csv%20.%5CSingleDistroGroupsmtpaddresses.csv%0A%24EmailAddressesToAdd%20%7C%20ForEach-Object%20%7B%0A%24AliasOfGroup%20%3D%20%24_.Alias%0A%24AddressToAdd%20%3D%20%24_.EmailAddress%0A%24x500%20%3D%20%22x500%3A%22%2B%20%24distrotomovedetails.LegacyExchangeDN%0A%24GroupLookup%20%3D%20Get-DistributionGroup%20-Identity%20%24AliasOfGroup%0ASet-DistributionGroup%20-Identity%20%24GroupLookup.Identity%20-EmailAddresses%20%40%7BAdd%3D%24AddressToAdd%7D%20-Verbose%0A%7D%0A%0A%23Add%20Group%20Members%20Back%20to%20Group%0AWrite-Host%20-ForegroundColor%20Magenta%20%22Adding%20Members%20Back%20to%20Group%22%0A%24GroupMembers%20%3D%20Import-Csv%20.%5CGroupmembersBeforeDel.csv%0A%24GroupMembers%20%7C%20ForEach-Object%20%7B%0A%24Distroprimarysmtp%20%3D%20%24_.DistributionGroup%0A%24MemberPrimarySMTP%20%3D%20%24_.EmailAddress%0AWrite-Host%20%22Adding%20%24memberprimarysmtp%20to%20%24Distroprimarysmtp%22%0AAdd-DistributionGroupMember%20-Identity%20%24Distroprimarysmtp%20-Member%20%24MemberPrimarySMTP%0A%7D%0A%0A%23Confirm%20Group%0AGet-DistributionGroup%20%24distroprimarysmtp%20%7C%20Select%20Alias%2CPrimarySmtpAddress%2CManagedBy%2CIsDirsynced%2CWhenCreated%20%7C%20FT%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

 

* Hybrid setup (Exchange 2013 - Single Server)

* All users already migrated to EXO

* its time to migrate distribution groups (Almost 200) - Already synced using AAD

* At present IT is controlling distribution groups and we want the same way

 

I would like to know what is the best way to migrate distribution group to EXO.   Is there any latest script / method  ?  I have found few years old discussion but not very clear to me.  

 

Thanks in advance

       

5 Replies

@SASK2381 Hi, since you have on-prem AD and are syncing using AD connect, you actually don't migrate your DLs. AD is the source of user data including users and groups, so you continue to manage them using AD users and computers, and Exchange admin center (on-prem). That includes mailbox management, creating mailboxes, manage DL membership etc.

@CloudHal  Thanks for response,

 

yes that's true right now , but we want to get rid of on-prem exchange .  Do you think it will work fine when we turn off on-prem exchange server ?  and we just able to manage them from AD ?

 

 

What you are suggesting is possible but unsupported. You can manage email attributes using AD users and computers, but it is not so easy for support, and user accounts won’t have all the correct email attributes. For example managing proxy addresses is prone to error.
The only way of retaining AD at the moment is to also retain exchange for management.

@CloudHal  thanks,

 

so if I want to get rid of on-prem exchange , the only option is to recreate them all in EXO,  in that case what is the best method, script, best practice to follow ? 

Manually deletion and recreating is time consuming and also temporary disruptive as I have almost 200 Distributions group. 

@SASK2381 

 

######## Global Variables ############
Set-Location -Path $env:USERPROFILE\Documents
#Get-PSSession | Remove-PSSession
$UserCredential = Get-Credential
$ListOfGroupsToMigrate = Import-csv .\GroupMigration.csv
#######################################
$ListOfGroupsToMigrate | ForEach-Object {
$SingleGroupToMigratePSMTP = $_.GroupEmailAddress
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://EXCHSVRFQDN.com/PowerShell/ -Authentication Kerberos -Credential $UserCredential
Import-PSSession $Session -Allowclobber
$distrotomovedetails = Get-DistributionGroup -Identity $SingleGroupToMigratePSMTP 
$distrotomovedetails | Export-csv .\singlegroupdetailsbeforedelete.csv -NoTypeInformation
$distrotomovedetails | ForEach-Object{
    $DistributionGroupName = $_.PrimarySmtpAddress
    Get-DistributionGroupMember -Identity $DistributionGroupName | ForEach-Object{
        [PSCustomObject]@{
            DistributionGroup = $DistributionGroupName
            MemberName = $_.DisplayName
            EmailAddress = $_.PrimarySmtpAddress
           #Other recipient properties here
        }
    }
} | Export-csv .\GroupmembersBeforeDel.csv -NoTypeInformation

#########################################
$groupowner = $distrotomovedetails.ManagedBy
$GroupOwnerCorrected = $groupowner.split('/')[-1]
$GroupOwnerCorrected.ToString()
$Managedby = Get-ADuser -Filter "displayName -eq '$GroupOwnerCorrected'"
$distrotomovedetails | fl

# Extract All Email Addresses for single group one per line so you can easily add back in EXO
$addresses = @()
    Foreach ($mbx in $distrotomovedetails) {
    Foreach ($address in $mbx.EmailAddresses) {
    $obj = "" | Select-Object Identity,Alias,EmailAddress,RecipientType
    $obj.Alias = $mbx.Alias
    $obj.Identity = $mbx.Identity
    $obj.RecipientType = $mbx.RecipientType
    $obj.EmailAddress = $address.ToString().SubString(0)
    $addresses += $obj 
        } } $addresses | Export-Csv .\SingleDistroGroupsmtpaddresses.csv -NoTypeInformation
$addresses
#Remove OnPremises Group
Remove-DistributionGroup -Identity $distrotomovedetails.Identity -Confirm:$false -Verbose
Start-Sleep 120

#Create On-Premises Contact
$ExternalEmailAddress = $distrotomovedetails.Alias + "@TENANT.mail.onmicrosoft.com"
New-MailContact -DisplayName $distrotomovedetails.DisplayName -Alias $distrotomovedetails.Alias -ExternalEmailAddress $ExternalEmailAddress -OrganizationalUnit "OU=Contacts,OU=Exchange Accounts,DC=lge-mdr,DC=com"
Set-MailContact -Identity $distrotomovedetails.PrimarySmtpAddress -HiddenFromAddressListsEnabled $true

#Close Session with OnPremise Exchange
Get-PSSession | Remove-PSSession -Verbose

#Connect EXO
Import-Module ExchangeOnlineManagement
Connect-ExchangeOnline -Credential $UserCredential
Write-Host -ForgroundColor Cyan "Running AAD Connect Sync"
#Remotely Run AAD Connect Delta Sync
$session = New-PSSession -ComputerName AADConnectSVRFQDN.com
Invoke-Command -Session $session -ScriptBlock {Import-Module -Name 'ADSync'}
Invoke-Command -Session $session -ScriptBlock {Start-ADSyncSyncCycle -PolicyType Delta}
Remove-PSSession $session
#Pause until AAD Connect Sync can run and Azure AD to catch up
Write-Host -ForgroundColor Green "Pausing for AAD Connect to run and then we check EXO for the group"
function Start-Sleep($seconds) {
    $doneDT = (Get-Date).AddSeconds($seconds)
    while($doneDT -gt (Get-Date)) {
        $secondsLeft = $doneDT.Subtract((Get-Date)).TotalSeconds
        $percent = ($seconds - $secondsLeft) / $seconds * 100
        Write-Progress -Activity "Sleeping" -Status "Sleeping..." -SecondsRemaining $secondsLeft -PercentComplete $percent
        [System.Threading.Thread]::Sleep(500)
    }
    Write-Progress -Activity "Sleeping" -Status "Sleeping..." -SecondsRemaining 0 -Completed
}

Start-Sleep 200
Write-host -ForegroundColor Cyan "Verifing the group  is gone from Azure AD, command will proceed once group is not showing in EXO" $distrotomovedetails.PrimarySmtpAddress
while($GetDistroFinder -ne $null){
Write-Host -ForegroundColor Cyan "Checking EXO for the old synced Distro Group"
$GetDistroFinder = Get-DistributionGroup -Identity $distrotomovedetails.PrimarySmtpAddress
Start-Sleep 10
}
Write-Host -ForegroundColor Green "Group Appears to have been removed please verify on Azure AD"
Read-Host -Prompt "Hit Enter To Proceed"

############ Recreate Group in the cloud ###########################################################################################################################################
Write-Host -ForegroundColor Cyan "Recreating the group in EXO"
#Create the EXO Distro group
New-DistributionGroup -DisplayName $distrotomovedetails.DisplayName -Alias $distrotomovedetails.Alias -Name $distrotomovedetails.Name -MemberDepartRestriction $distrotomovedetails.MemberDepartRestriction -MemberJoinRestriction $distrotomovedetails.MemberJoinRestriction -PrimarySmtpAddress $distrotomovedetails.PrimarySmtpAddress -RequireSenderAuthenticationEnabled:$false -ManagedBy $Managedby.UserPrincipalName -Verbose

#Add additional SMTP and x500 addresses to the group.
Write-Host -ForegroundColor Green "Adding SMTP and x500 Addresses Back to" $distroprimarysmtp
$EmailAddressesToAdd = Import-Csv .\SingleDistroGroupsmtpaddresses.csv
$EmailAddressesToAdd | ForEach-Object {
$AliasOfGroup = $_.Alias
$AddressToAdd = $_.EmailAddress
$x500 = "x500:"+ $distrotomovedetails.LegacyExchangeDN
$GroupLookup = Get-DistributionGroup -Identity $AliasOfGroup
Set-DistributionGroup -Identity $GroupLookup.Identity -EmailAddresses @{Add=$AddressToAdd} -Verbose
}

#Add Group Members Back to Group
Write-Host -ForegroundColor Magenta "Adding Members Back to Group"
$GroupMembers = Import-Csv .\GroupmembersBeforeDel.csv
$GroupMembers | ForEach-Object {
$Distroprimarysmtp = $_.DistributionGroup
$MemberPrimarySMTP = $_.EmailAddress
Write-Host "Adding $memberprimarysmtp to $Distroprimarysmtp"
Add-DistributionGroupMember -Identity $Distroprimarysmtp -Member $MemberPrimarySMTP
}

#Confirm Group
Get-DistributionGroup $distroprimarysmtp | Select Alias,PrimarySmtpAddress,ManagedBy,IsDirsynced,WhenCreated | FT
}