Use PowerShell to automate your file share to OneDrive for Business migration.
Index
Prerequisites
- Full control on your DOMAIN folder.
- The DOMAIN folder is mapped as a network drive.
- Global Administrator or SharePoint Administrator permissions are required.
- Your OneDrives have been provisioned.
- This can be automated with the Get-OneDriveURL cmdlet.
- Your Tenant is connected in the ShareGate Desktop Explorer.
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
- Generate a list of the names of all the directories contained in the DOMAIN folder using this script:
Note: You have to replace the paths with your own.dir –Directory Z:\ -Name | Out-File C:\MigrationPlanning\onedrivemigration.csv
- Open the CSV file generated from the script in Excel.
- Insert a new row at the top of the results for the headers.
- Name the first column DIRECTORY.
- Insert the mapped drive letter before the names of the directories in your columns (i.e.: Z:).
- Open ShareGate Desktop.
- 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 Tenant.
- Click on Run.
- Click on Export in the top right corner.
- Save the file on your drive, and open it.
- Replace the Site address header by ONEDRIVEURL.
- Combine the two spreadsheets so that each directory corresponds the the correct URL in your rows.
Note: If sorting alphabetically doesn't work, you can reorganize your data manually or you can look for a solution with 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:\MigrationPlanning\onedrivemigration.csv" $table = Import-Csv $csvFile -Delimiter ","
$mypassword = ConvertTo-SecureString "mypassword" -AsPlainText -Force Set-Variable dstSite, dstList foreach ($row in $table) { Clear-Variable dstSite Clear-Variable dstList $dstSite = Connect-Site -Url $row.ONEDRIVEURL -Username "myusername" -Password $mypassword
Add-SiteCollectionAdministrator -Site $dstSite $dstList = Get-List -Name Documents -Site $dstSite Import-Document -SourceFolder $row.DIRECTORY -DestinationList $dstList
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 column items. Make sure your script uses the same delimiter as your file (a quick way to verify this, is by opening the CSV with Notepad).
- $mypassword: Replace "mypassword" by your Microsoft 365 admin account password.
-
$dstSite: Replace "myusername" by your Microsoft 365 admin account user name.
- 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.
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.
Comments
9 comments
This is a useful start - what would be great is some other examples for other scenarios e.g. incremental update ?
Hello Dorje,
Thank you so much for the feedback. It is a great idea - we are looking into ways we can improve our PowerShell documentation with more step-by-steps! In the meantime, we think the New Copy Settings and Incremental Update articles will help.
Have a great day!
I know it is possible to add a time filter with Sharepoint migration, but it also possible to add a time filter for onedrive migration within the Powershell script mentioned above?
Hello Marcel!
Yes, you can, you'll want to use the New Property Template script.
Example:
$propertyTemplate = New-PropertyTemplate -AuthorsAndTimestamps -VersionHistory -Permissions -WebParts -NoLinkCorrection -FlattenFolders -VersionLimit 5 -CheckInAs Publish -ContentApproval SameAsCurrent - From YYYY-MM-DD -To YYYY-MM-DD
Hope that helps!
Hello Alex,
We use this script to import the home-folders from our users to OneDrive for Business.
But we want to skip certain folders inside the home-folders. So, for each home-folder I cycle through all subfolders, selecting which to copy and which not. Then I use this line:
$Result = Import-Document -SourceFolder "$SourcePath" -DestinationList $DstList -DestinationFolder "$DstFolder" -CopySettings $CopySettings -InsaneMode
As you can see I added the option -DestinationFolder "$DstFolder"
This returns the message "The folder X/ does not exist in the destination."
That's correct, but it has to be created. Unfortunately, I didn't find an option to force that.
So, my question is: Is there an option to create a folder that doesn't exist in the destination?
Thanks in advance for your answer!
Erik Knibbe
Hey Erik!
We are not aware of an option for that! If you do end up finding a workaround, please feel free to share it.
Thanks!
Hello Erik
Did you ever find a solution for this (to create a new folder for each user)? It looks like SPMT provided by Microsoft has the ability to create folder on the fly during the migration. I ran into some speed issues with that so have started to look at ShareGate but I'm surprised that ShareGate doesn't natively support this.
One way I found (probably not the most elegant) is by using the SharePoint online module:
Install-Module SharePointPnPPowerShellOnline
$mypassword = ConvertTo-SecureString "Admin Pwd" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ("Admin User", $mypassword)
Connect-PnPOnline -Url https://ORDCODE-my.sharepoint.com/personal/user_1_domain_com/ -Credentials $mycreds
Add-PnPFolder -name NewFOlder -folder "Documents"
Hello Amit,
Thanks for your answer!
We didn't found a solution. Our dirty work-arround is to copy all folders, en tell the users that they can safely remove the special folders that aren't used. ;-)
Maybe the SharePoint module would have worked, but for now we already started the migration.
Greetings!
Erik
Greetings!
I'm looking to migrate network folders to user's OneDrives but want to migrate into a subfolder called "Network Drive".
I already created the subfolder within each OneDrive, but wondered how to copy the contents directly into the new subfolder.
Please sign in to leave a comment.