Use PowerShell to automate your SharePoint My Sites to OneDrive for Business migration.
Index
Prerequisites
You have Farm admin permissions in the source.
You have global admin or SharePoint admin permissions in the destination.
You are connected to your SharePoint central admin in Explorer.
You are connected to your Microsoft 365 admin center in Explorer.
Your OneDrives have been provisioned (this can be automated with the Get-OneDriveURL cmdlet).
Note: You need to be site collection admin on each My Site and OneDrive, even if you have higher admin privileges. In steps (10) and (25) below, you will use Farm admin, SharePoint admin, or global admin permissions to apply site collection admin permissions on all your My Sites and OneDrives. The PowerShell script will then remove the site collection admin permissions as you migrate each My Site.
How-to
Tip: To migrate your documents to a new folder in your OneDrives, see Walkthrough - Migrate to a new OneDrive folder in PowerShell.
Create a CSV guide for your migration
Go to All reports.
Click Create custom report in the top right corner.
Select Site collection as your object type.
Click Continue without saving.
Select the Web app you use for your My Sites.
Click Next.
Click Run now.
Select all your My Sites with the checkmark box at top of the list.
Click Edit in the Quick actions menu.
Select Add administrators in the Transformations dropdown.
Search and add your account in the Select user or group field that appears.
Click Apply.
Click Back two times to go back to your report results.
Click Export in the top right corner.
Save the file on your drive.
Go back to All reports.
Click Create custom report.
This time select OneDrive for Business as your object type.
Click Continue without saving.
Select the Microsoft 365 tenant you use for your OneDrives.
Click Next.
Click Run now.
Select all your OneDrives with the checkmark box at top of the list.
Click Edit in the Quick actions menu.
Select Add administrators in the Transformations dropdown.
Search and add your account in the Select user or group field that appears.
Click Apply.
Click Back two times to go back to your report results.
Click Export in the top right corner.
Save the file on your drive.
Open Excel.
Add titles SourceSite and DestinationSite to the first two columns.
Open the two files you saved in the previous steps.
Combine the data from both files into the new document so that the source URL corresponds to the destination URL under their respective column.
โ
If sorting alphabetically doesn't work, you can reorganize your data manually or you can look for a solution with Excel macros or PowerShell.Save this new file as a CSV on your drive.
Create your script
Copy and paste the following script into the PowerShell application of your choice.
Import-Module Sharegate$csvFile = "C:\CSV\CopyContent.csv"$table = Import-Csv $csvFile -Delimiter ","$srcUsername = "sourceusername"$srcPassword = ConvertTo-SecureString 'sourcepassword' -AsPlainText -Force$dstUsername = "destinationusername"$dstPassword = ConvertTo-SecureString 'destinationpassword' -AsPlainText -ForceSet-Variable srcSite, dstSite, srcList, dstListforeach ($row in $table) { Clear-Variable srcSite Clear-Variable dstSite Clear-Variable srcList Clear-Variable dstList $srcSite = Connect-Site -Url $row.SourceSite -Username $srcUsername -Password $srcPassword $dstSite = Connect-Site -Url $row.DestinationSite -Username $dstUsername -Password $dstPassword $srcList = Get-List -Site $srcSite -Name "Documents" $dstList = Get-List -Site $dstSite -Name "Documents" Copy-Content -SourceList $srcList -DestinationList $dstList Remove-SiteCollectionAdministrator -Site $srcSite Remove-SiteCollectionAdministrator -Site $dstSite}
Adjust your script so that it will work for you. Here are a few guidelines:
$csvFile: Adjust the path to point to the CSV file you saved before.
$table: The delimiter is the symbol your CSV uses to separate items in a row. Ensure your script uses the same delimiter as your file (a quick way to verify this is by opening the CSV with Notepad).
$srcUsername, $srcPassword, $dstUsername, and $dstPassword: Replace "sourceusername", 'sourcepassword', "destinationusername", and 'destinationpassword' with your Microsoft 365 credentials.
Connect-site: The command to connect to a SharePoint site. If you need to change the authentication method, see Connect Site.
Set-variable and Clear-Variable: These commands help prevent an issue where a connection failure can cause your data to end up in the wrong OneDrive.
foreach: We use foreach to loop through the values in your CSV file. You can find more about it here.
Remove-SiteCollectionAdministrator: This function removes your user account as site collection administrator on OneDrive after its migration. For more information, see Remove Site Collection Administrator.
Run your script once it's properly adjusted and tested.
Considerations
To use Browser authentication, see Use credentials from a previous browser connection.
Migration reports are automatically generated, and you can find them in Tasks. You can also export the reports in your script with Export-Report.
You can schedule your migration using PowerShell to run it off-hours and optimize performance.
Copy & Replace is used by default with this script. If you want to run an incremental migration, you can use the New-CopySettings command.
To replicate the copy options from ShareGate Migrate, see Copy options in PowerShell.
If you have hundreds of gigabytes of data to migrate, we recommend creating multiple CSVs to run the migration in smaller batches.