When you migrate to multiple OneDrives with a foreach loop statement, there is no way to create a folder through the ShareGate Desktop commands in PowerShell.
You can create a new folder in all your OneDrives before you run your PowerShell migration.
Note: With these instructions, you will create a CSV list of all your OneDrives. You can then reuse that same CSV list for your OneDrive migration.
Index
Prerequisites
- You have Global admin or SharePoint admin permissions.
- Your OneDrives have been provisioned (this can be automated with the Get-OneDriveURL cmdlet)
- You are connected to your Microsoft 365 Admin center in Explorer.
Script preparation
Create a folder on your local drive and a CSV list of all your OneDrives with the following steps:
- On your local drive, create a new folder.
- Name the folder as per your preference in your OneDrives (i.e. C:\Migrated data).
- Note of the path of the folder.
- In 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 source Tenant.
- Click on Run.
- Click on Export in the top right corner.
- Save the file on your drive.
- Open the report in Excel.
- Add the title ONEDRIVEURL to the first column.
- Save this new file as a CSV on your drive (i.e. C:\foldermigration.csv).
- Note the path of the CSV file.
Create your script
Copy and paste the following script in the PowerShell application of your choice.
$csvFile = "C:\foldermigration.csv"
$table = Import-Csv $csvFile -Delimiter ","
$MyPassword = ConvertTo-SecureString "My password" -AsPlainText -Force
$MyUsername = "My username"
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 -SourceFilePath "C:\Migrated data" -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 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).
- $MyPassword and $MyUsername: Replace "My password" and "My username" 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.
- -SourceFilePath: Replace "C:\Migrated data" with the path of the folder you created at step (2).
- 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 is property adjusted and tested.
Once the folder is added to all the OneDrives, add -DestinationFolder "Migrated data" to the copy line of your OneDrive migration script (Replace "Migrated data" by the name of the folder you created).
For example, the copy line could look like this in your migration script:
Import-Document -SourceFolder $row.DIRECTORY -DestinationList $dstList -DestinationFolder "Migrated data"