Recently I was tasked with recreating 500+ email distribution groups from our companies local exchange servers up to 365. Recreating over 500 distribution groups by hand would take weeks and an extreme number of raw man-hours, so there was only one solution, powershell scripting! This is a quick guide to how I scripted the recreation, and re-adding of members from a local exchange 2010 server to 365.
First log into the local exchange server and spin up a new instance of exchange management shell. We’ll be exporting the existing distribution groups to a csv file for easy import later. Run the below command to generate a csv file listing all distribution group members and their email addresses as preparation for import into 365.
$( foreach ($group in Get-DistributionGroup) { get-distributiongroupmember $group | select-object @{Name='Group';Expression={$group.Name}}, PrimarySmtpAddress } ) | export-csv C:\DistributionGroupMembers.csv -notype
If you see an error/warning about a corrupted distribution group, go into exchange management console and remove any blank spaces in the alias for that group. Then delete the export csv and try exporting again.
We’ll want to edit the csv file, flipping the group member email address and group name columns, we’ll also want to edit line 1 to say UPN in column A cell 1 and GroupName in column B cell 1. This is for later importing. Column A should be group member email addresses and column B should be the distribution group name the email belongs to.
Next lets export the distribution groups themselves, along with their email addresses and display names to csv:
Get-DistributionGroup | select Name,DisplayName,PrimarySmtpAddress | Export-Csv C:\Distributiongroups.csv -notype
Now that we have our groups, and their members, we’ll want to un-sync the distribution groups with 365 (if you had them syncing via dirsync). Move them into an AD OU that is excluded from 365 sync and wait for the resulting sync (or force it) before continuing.
Once the sync is complete and the groups are no longer showing in 365 admin panel its time to create their 365 version by importing the previously created csv files.
First lets spin up a new 365 powershell instance. Use Azure active directory module powershell for this.
Set-ExecutionPolicy Unrestricted -Force $O365CREDS = Get-Credential $SESSION = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $O365CREDS -Authentication Basic -AllowRedirection Import-PSSession $SESSION Connect-MsolService -Credential $O365CREDS
After the session is up, head to the C:\ drive, where you should have placed your exported csv files. Run the below command to import your distribution group csv file and have it create your 365 distribution groups:
$import = Import-Csv -Path "C:\Distributiongroups.csv" foreach ($item in $import) {New-DistributionGroup -Name $item.Name -DisplayName $item.DisplayName -PrimarySmtpAddress $item.PrimarySmtpAddress | Export-Csv -Path "C:\temp\New-DistributionGroup_LogFile_$(get-date -Format ddMMyyyy).csv"}
If you get any errors while running the above command, you can simply correct them in the csv file and run it again, any successful distribution group importing will be skipped with a ‘already exists’ error..
Finally we need to import the members into the distribution groups. Run the below command to import your group member csv file and upload to 365.
$import = Import-Csv -Path "C:\DistributionGroupMembers.csv" foreach ($item in $import) {Add-DistributionGroupMember -Identity $item.GroupName -Member $item.UPN -Verbose | Out-File -FilePath "C:\temp\Add-DGMembers_LogFile_$(get-date -Format ddMMyyyy).log"}
And your done! You can now either delete your local distribution groups or just leave them in place and flip all MX records to 365.