My scripts to do a OneDrive and Teams (with SharePoint) tenant to tenant migration with ShareGate

Here is a collection of the scripts I use to do OneDrive and Teams migrations from tenant to tenant. If there is separate SharePoint content on the sites connected to the team, this is also migrated over.

The docs from ShareGate have a lot of great information on the various PowerShell commands, but there are not always complete examples of the two migration scenarios above. So the scripts below are created based on the examples from ShareGate.

Reminder: For running these migrations you need to have at least Teams and SharePoint admin rights. The admin user also needs a Teams license in both tenants. You also need to log in to Teams at least once in both tenants before starting the migration.

This script is used to get the OneDrive URL for the users in both tenants. It will also provision OneDrive in the destination tenant if it does not exist. In that case, you need to run this twice with some time in between while provisioning finishes.

$tenantsource = Connect-Site -Url "https://contoso-admin.sharepoint.com" -Browser
$tenantdest = Connect-Site -Url "https://tailspin-admin.sharepoint.com" -Browser
$UPNDomainSource = "contoso.onmicrosoft.com"
$UPNDomainDestination = "tailspin.onmicrosoft.com"
#List of users in the old tenant that are to be migrated. Header in CSV is UPN.
$users = import-csv 'C:\temp\UPN.csv'
$filepath = "C:\temp\CopyContentOneDrive.csv"
# Header for CSV file
"UPN,SourceSite,DestinationSite" >> $filepath
foreach($user in $users){
$userOneDriveURLSource = @()
$userOneDriveURLSource = Get-OneDriveUrl -Tenant $tenantsource -Email $user.UPN
$userUPN = @()
$userUPN = ($User.upn).replace("$UPNDomainSource","$UPNDomainDestination")
$userOneDriveURLDestination = @()
$userOneDriveURLDestination = Get-OneDriveUrl -Tenant $tenantdest -Email $userUPN -ProvisionIfRequired -DoNotWaitForProvisioning
"$($user.upn),$userOneDriveURLSource,$userOneDriveURLDestination" >> $filepath
}

This script add your admin/migration user as site collection admin for the OneDrive´s.

$Sites = import-csv c:\temp\CopyContentOneDrive.csv -Delimiter ","
$tenantsource = Connect-Site -Url "https://contoso-admin.sharepoint.com" -Browser
foreach($site in $Sites){
Add-SiteCollectionAdministrator -SiteUrl $site.SourceSite -CentralAdmin $tenantsource
}
$tenantdest = Connect-Site -Url "https://tailspin-admin.sharepoint.com" -Browser
foreach($site in $Sites){
Add-SiteCollectionAdministrator -SiteUrl $site.DestinationSite -CentralAdmin $tenantdest
}

This script starts the OneDrive migration.

Import-Module Sharegate
$csvFile = "C:\temp\CopyContentOneDrive.csv"
$table = Import-Csv $csvFile -Delimiter ","
$tenantsource = Connect-Site -Url "https://contoso-admin.sharepoint.com" -Browser
$tenantdest = Connect-Site -Url "https://tailspin-admin.sharepoint.com" -Browser
$copysettings = New-CopySettings -OnContentItemExists IncrementalUpdate
Set-Variable srcSite, dstSite, srcList, dstList
foreach ($row in $table) {
Clear-Variable srcSite
Clear-Variable dstSite
Clear-Variable srcList
Clear-Variable dstList
$srcSite = Connect-Site -Url $row.SourceSite -UseCredentialsFrom $tenantsource
$dstSite = Connect-Site -Url $row.DestinationSite -UseCredentialsFrom $tenantdest
#The name of the folder is dependant on what language you use in your tenant.
#Run Get-List -Site $srcSite to see what the name of the documents foldr is
$srcList = Get-List -Site $srcSite -Name "Documents"
$dstList = Get-List -Site $dstSite -Name "Documents"
Copy-Content -SourceList $srcList -DestinationList $dstList -CopySettings $copysettings
#comment out bellow commands if you plan on running a incremental copy later.
Remove-SiteCollectionAdministrator -Site $srcSite
Remove-SiteCollectionAdministrator -Site $dstSite
}

This script migrates Teams teams with SharePoint content connected to them.

$csvFile = "C:\temp\teamslist.csv"
#List of team object IDs. Header in CSV is ID.
$table = Import-Csv $csvFile -Delimiter ","
$source = Connect-Tenant -Domain contoso.onmicrosoft.com -Browser
$destination = Connect-Tenant -Domain tailspin.onmicrosoft.com -Browser
foreach ($row in $table) {
$team = Get-Team -id $($row.id) -Tenant $source
Copy-Team -Team $team -DestinationTenant $destination
}

This script runs an incremental migration of teams. It copies new/edited content that’s different from the first run.

$source = Connect-Tenant -Domain contoso.onmicrosoft.com -Browser
$destination = Connect-Tenant -Domain tailspin.onmicrosoft.com -Browser
#Enter the date from when the sesion you want to run incremental on is from.
$sessions = Find-CopySessions -From "11/25/2022"
$sessions | ForEach-Object { Copy-TeamIncremental -SessionId $_.Id -SourceTenant $source -DestinationTenant $destination }

One thought on “My scripts to do a OneDrive and Teams (with SharePoint) tenant to tenant migration with ShareGate

  1. […] Here is a collection of the scripts I use to do OneDrive and Teams migrations from tenant to tenant. If there is separate SharePoint content on the sites connected to the team, this is also migrated over.The docs from ShareGate have a lot of great information on the various PowerShell commands, but there are not… — Read on alexholmeset.blog/2022/11/25/my-scripts-to-do-a-onedrive-and-teams-with-sharepoint-tenant-to-tenant-… […]

    Like