Use PowerShell to automate your OneDrive for Business to OneDrive for Business migration.
Index
Prerequisites
-
Global Administrator or SharePoint Administrator permissions are required.
-
Your OneDrives have been provisioned.
- This can be automated with the Get-OneDriveURL cmdlet.
Note: For a OneDrive migration the required permission is site collection administrator. The script in this article has commands to add site collection administrator permissions at the beginning of a OneDrive migration and to remove the permissions after it is completed. You need Global Administrator or SharePoint Administrator permissions for the commands to work.
Create a CSV guide for your migration
- Go to All reports.
- Click on Create custom report in the top right corner.
- Select OneDrive for Business
as your object type.
- Click Continue without saving.
- Select your source Tenant.
- Click on Run.
- Click on Export in the top right corner.
- Save the file on your drive.
- Repeat steps (1) to (8), selecting your destination tenant at step (5).
- Open Excel.
- Add titles SourceSite and DestinationSite to the first two columns.
- Open the two files you saved on step (8).
- Combine the data from both the files into the new document so that the source URL corresponds to the destination URL under their respective column.
Note: 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 in 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 -Force
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 -Username $srcUsername -Password $srcPassword
Add-SiteCollectionAdministrator -Site $srcSite
$dstSite = Connect-Site -Url $row.DestinationSite -Username $dstUsername -Password $dstPassword
Add-SiteCollectionAdministrator -Site $dstSite
$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 so that it points to the CSV file you saved before.
- $table: The delimiter is the symbol your CSV uses to separate your items in a row. Make sure 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" by your matching Microsoft 365 credentials.
- Connect-Site: If you need to change the authentication method, you will find how to do it in the Connect Site article.
- 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. If you have to use the browser connection method, you will have to remove these lines.
- foreach: We use foreach to loop through the values in your CSV file. You can find more about it here.
- Add-SiteCollectionAdministrator and Remove-SiteCollectionAdministrator: Adds your user account as site collection administrator before your migration, and removes it after. The site collection administrator permission is required during the migration.
Run your script once it's property adjusted and tested.
Notes:
- To migrate your documents to a new folder in your OneDrives, you must create the folder beforehand.
- Migration reports are automatically generated, and you will be able to 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.
- To perform an incremental migration, check the the Incremental update using PowerShell article.
- If you have hundreds of gigabytes of data to migrate, we recommend creating multiple CSVs to run the migration in smaller batches.