When you migrate to multiple OneDrives with a foreach loop statement, there is no way to create a folder through the ShareGate migration tool commands in PowerShell.
You can however create a new folder in all your OneDrives before you run your PowerShell migration.
Tip: 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.
Note: You need to be site collection admin on each OneDrive, even if you have higher admin privileges. In step (12) below, you will use SharePoint admin or global admin permissions to apply site collection admin permissions needed to create a folder in each OneDrive. We do not go through the process of removing the site collection admin permissions in this guide.
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 the ShareGate migration tool 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.
- 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 get back to your report results.
- 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 second 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
$dstList = Get-List -Name Documents -Site $dstSite
Import-Document -SourceFilePath "C:\Migrated data" -DestinationList $dstList
}
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" 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. To use Browser authentication, see Use credentials from a previous browser connection.
- 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.
- -SourceFilePath: Replace "C:\Migrated data" with the path of the folder you created at step (2).
- Remove-SiteCollectionAdministrator: Removes your user account as site collection administrator on the OneDrive after its migration. For more information, see Remove Site Collection Administrator.
Run your script once it is properly 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" with 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"
Note: -DestinationFolder
cannot be used to create new folders at the destination.