Forum Discussion

Curious_Kevin16's avatar
Curious_Kevin16
Iron Contributor
Jun 11, 2023

Export DL Memberships from Exchange 2013 and Import to 365 via PowerShell

Hi Guys, 

 

I am migrating our Distribution lists from Exchange 2013 server to O365. I have the following script to export and import memberships to a SINGLE DL but seeking your kind help to do the same for MULTIPLE DLs (200 in my case). Appreciate if anyone can shed some lights with a modified script here !

 

Export from Exchange Server 2013 for a single DL: 

 

Get-DistributionGroupMember -Identity "Marketing USA" | select Name,Alias | Export-CSV -Path "C:\Temp\ExportedDLMembers.csv"

 

Import DL Membership from above CSV to Office 365 for a single DL: 

 

$CSVPath = "C:\Temp\ExportedDLMembers.csv"
$Memberships = Import-Csv -Path $CSVPath
$Memberships | ForEach-Object {
$DistributionList = $_.Name
$Member = $_.PrimarySmtpAddress
Add-DistributionGroupMember -Identity $DistributionList -Member $Member
}

 

How can I achieve the same for MULTIPLE DLs (Export from Exchange server 2013 and import to 365 where Destination DLs are already exists).  ?

 

 

Thank you so much !

Kev

2 Replies

  • AndySvints's avatar
    AndySvints
    Steel Contributor

    Hello Curious_Kevin16,

    Script provided by elieelkarkafi is for exporting data from M365 and will not fit directly to your use case.

    In your case logic is pretty straight forward:
    Exchange Server
    1. Get All Distribution Lists
    2. For each group  export Members

    M365
    1. For each Exchange server DL get exported members
    2. Foreach member add him/her to DL in M365

     

    Example code:

    #Exchange Server
    $DistributionList=Get-DistributionGroup
    foreach($dl in $DistributionList){
     Get-DistributionGroupMember -Identity $dl.Name | Export-CSV -Path "C:\Temp\$($dl.Name)-DLMembers.csv"
    }
    
    #M365
    #Connect to Exchange Online
    $DistributionList=Get-ChildItem -Path "C:\Temp" -Filter "*-DLMembers.csv"
    foreach($dl in $DistributionList){
     $Memberships = Import-Csv -Path $($dl.Name)-DLMembers.csv"
     foreach($m in $Membership){
      Add-DistributionGroupMember -Identity $dl.Name -Member $m.PrimarySmtpAddress
     }
    }

    Please note that this sample code will not handle expanding group which is a member of another group.

    Hope that helps. 

Resources