This page is collection of powershell “oneliners” that I've used to migrate items from Office 365 tenant to another
Exporting classic distribution groups to .csv files
Connect-ExchangeOnline Get-DistributionGroup -ResultSize Unlimited | Select-Object DisplayName,PrimarySmtpAddress,@{Name="EmailAddresses";Expression={$_.EmailAddresses | Where-Object {$_ -clike "smtp*"}}},RequireSenderAuthenticationEnabled | Export-Csv -Encoding UTF8 -NoTypeInformation C:\temp\exported_distributionlists.csv
And importing distribution lists. If importing another tenant, remember to clean onmicrosoft.com aliases and rename domains if needed.
Import-Csv C:\temp\distribution_list_export.csv | foreach { $proxyaddresses = $_.EmailAddresses -split ' ' $managers = $_.ManagedBy -split ' ' New-DistributionGroup -Name $_.DisplayName -ManagedBy $managers -PrimarySmtpAddress $_.PrimarySmtpAddress -RequireSenderAuthenticationEnabled $_.RequireSenderAuthenticationEnabled -WhatIf Set-DistributionGroup -Identity $_.DisplayName -EmailAddresses @{add= $proxyaddresses} -WhatIf }
Exporting distribution lists members. I borrowed this from Internet. Problem is that this script does not output nice .csv file, needs to be redesigned.
Connect-ExchangeOnline $objDistributionGroups = Get-DistributionGroup -ResultSize Unlimited Foreach ($objDistributionGroup in $objDistributionGroups) { write-host "Processing $($objDistributionGroup.DisplayName)..." #Get members of this group $objDGMembers = Get-DistributionGroupMember -Identity $($objDistributionGroup.PrimarySmtpAddress) write-host "Found $($objDGMembers.Count) members..." #Iterate through each member Foreach ($objMember in $objDGMembers) { Out-File -FilePath C:\temp\exported_distributiongroup_members.csv -InputObject "$($objDistributionGroup.DisplayName),$($objDistributionGroup.PrimarySMTPAddress),$($objMember.DisplayName),$($objMember.PrimarySMTPAddress),$($objMember.RecipientType)" -Encoding UTF8 -append } }
Lets say you add headers to file produced on previous script for example “DisplayName,PrimarySMTPAddress,User,UserPrimarySMTP,Mailboxtype” (Add first line on file)
You can import members to new tenant with following command
Import-Csv C:\temp\distributiongroup_importmembers.csv | foreach { Add-DistributionGroupMember -Identity $_.DisplayName -Member $_.UserPrimarySMTP }
Yet again pay attention to UPN if it needs to be changed before importing.