Skip to main content
All CollectionsMigratePowerShellOneDrive migrations
Create a new OneDrive folder for migrations with PowerShell
Create a new OneDrive folder for migrations with PowerShell
Updated this week

You cannot directly provision a new folder in your OneDrives when you run one of our PowerShell scripts to copy content to many OneDrives.

With this guide, you can provision a new folder in your OneDrives before running your migration script.

Index

Prerequisites

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:

  1. On your local drive, create a new folder.

  2. Name the folder as per your preference in your OneDrives (e.g., C:\Migrated data).

  3. Note of the path of the folder.

  4. In ShareGate Migrate, go to All reports.

  5. Click Create custom report in the top right corner.

  6. Select OneDrive for Business as your object type.

  7. Click Continue without saving.

  8. Select your tenant.

  9. Click Run.

  10. Select all your OneDrives with the checkmark box at top of the list.

  11. Click Edit in the Quick actions menu.

  12. Select Add administrators in the Transformations dropdown.

  13. Search and add your account in the Select user or group field that appears.

  14. Click Apply.

  15. Click Back two times to get back to your report results.

  16. Click Export in the top right corner.

  17. Save the file on your drive.

  18. Open the report in Excel.

  19. Add the title ONEDRIVEURL to the second column.

  20. Save this new file as a CSV on your drive (i.e. C:\foldermigration.csv).

  21. Note the path of the CSV file.

Tip: You can reuse that same CSV list for your OneDrive migration.

Create your script

Copy and paste the following script into 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, dstListforeach ($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 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).

  • $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 folder path you created in 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.

Migrate content to your new folders

To migrate your content to the new OneDrive folders, you must add -DestinationFolder "folder name" (replace folder name with the name of your folder) to the copy line of your OneDrive migration script.

The copy line of your migration script should look like this:

Import-Document -SourceFolder $row.DIRECTORY -DestinationList $dstList -DestinationFolder "Migrated data"
Did this answer your question?