Skip to main content
All CollectionsMigratePowerShellOneDrive migrations
Import from file share to OneDrive for Business with PowerShell
Import from file share to OneDrive for Business with PowerShell
Updated this week

Use PowerShell to automate your file share to OneDrive for Business migration.

Index

Prerequisites

Note: You must be site collection admin on each OneDrive, even if you have higher admin privileges. In step (15), you will use SharePoint admin or global admin permissions to apply site collection admin permissions on all your OneDrives. The PowerShell script will then remove the site collection admin permissions as you migrate each OneDrive.

How-to

Tip: To migrate your documents to a new folder in your OneDrives, see Walkthrough - Migrate to a new OneDrive folder in PowerShell.

Create a CSV guide for your migration

  1. Generate a list of the names of all the directories contained in the DOMAIN folder using this script:

    dir –Directory Z:\ -Name | Out-File C:\MigrationPlanning\onedrivemigration.csv

    Note: You have to replace the paths with your own.

  2. Open the CSV file generated from the script in Excel.

  3. Insert a new row at the top of the results for the headers.

  4. Name the first column DIRECTORY.

  5. Insert the mapped drive letter before the names of the directories in your columns (i.e.: Z:).

  6. Open ShareGate Migrate.

  7. Go to All reports.

  8. Click on Create custom report in the top right corner.

  9. Select OneDrive for Business as your object type.

  10. Click Continue without saving.

  11. Select your tenant.

  12. Click Run.

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

  14. Click Edit in the Quick actions menu.

  15. Select Add administrators in the Transformations dropdown.

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

  17. Click Apply.

  18. Click Back two times to go back to your report results.

  19. Click Export in the top right corner.

  20. Save the file on your drive, and open it.

  21. Replace the Site address header with ONEDRIVEURL.

  22. Combine the two spreadsheets so that each directory corresponds to the correct URL in your rows.

    Create csv file for OneDrive for Business Migration

    If sorting alphabetically doesn't work, you can reorganize your data manually, or you can look for a solution with macros or PowerShell.

  23. 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
    $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' with your Microsoft 365 admin account password.

  • $dstSite: Replace "myusername" with your Microsoft 365 admin account user name.

  • Connect-site: The command to connect to a SharePoint site. If you need to change the authentication method, see Connect Site.

  • 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.

  • 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's properly adjusted and tested.

Considerations

Did this answer your question?