Use PowerShell to automate migrations from file share to OneDrive for Business.
Note: At the end of January 2026, Microsoft began deprecating the IDCRL cookie, meaning that only Browser and Modern authentication methods with the parameters -Browser and -ModernAuth will work in most Microsoft 365 tenants. To learn more about this change, see Important change to "Other user" authentication for Microsoft 365.
Prerequisites
You have the default Read NTFS Permission or higher on your file share.
Global administrator or SharePoint administrator permissions are required.
Your OneDrives have been provisioned (this can be automated with the Get-OneDriveURL cmdlet).
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 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
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
Replace the paths with your own.
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 Migrate.
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 Run.
Select all your OneDrives with the checkmark box at the top of the list.
Click Edit in the Quick actions menu.
Select Add administrators in the Transformations dropdown.
Search for and add your account in the Select user or group field that appears.
Click Apply.
Click Back two times to go back to your report results.
Click Export in the top right corner.
Save the file on your drive, and open it.
Replace the Site address header with ONEDRIVEURL.
Combine the two spreadsheets so that each directory corresponds to the correct URL in your rows.
If sorting alphabetically doesn't work, you can reorganize your data manually or look for a solution using macros or PowerShell.
Save this new file as a CSV on your drive.
Create your script
Copy and paste the following script into the PowerShell application of your choice.
Import-Module Sharegate
# Define the CSV file path
$csvFile = "C:\MigrationPlanning\onedrivemigration.csv"
# Import the CSV file
$table = Import-Csv $csvFile -Delimiter ","
# Define the destination connection
$dstSiteConnection = Connect-Site -Url "https://destinationtenantname-my.sharepoint.com/" -ModernAuth
# Set variables for site and list operations
Set-Variable dstSite, dstList
# Loop through each row in the CSV
foreach ($row in $table) {
# Clear previous values of variables
Clear-Variable dstSite
Clear-Variable dstList
# Connect to the OneDrive site
$dstSite = Connect-Site -Url $row.ONEDRIVEURL -UseCredentialsFrom $dstSiteConnection
# Get the "Documents" list from the destination site
$dstList = Get-List -Name Documents -Site $dstSite
# Import documents from the source folder to the destination list
Import-Document -SourceFolder $row.DIRECTORY -DestinationList $dstList
# Remove site collection administrator permissions
Remove-SiteCollectionAdministrator -Site $dstSite
}
Adjust your script to 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 to open the CSV in Notepad).
$dstSiteConnection: Replace the URL with the root URL of your OneDrives in your destination tenant.
Connect-site: The command to connect to a SharePoint site or OneDrive for Business. If you need to change the authentication method from Modern authentication, see Connect Site.
-UseCredentialsFrom: This script uses Modern authentication with the -UseCredentialsFrom parameter to reuse a single connection. This script will prompt you for your Microsoft 365 credentials when you start it. If MFA isn't enforced on your tenants, you can set your script to use a username and password for unattended scheduling. To learn more, see Connect-Site and Avoid repeatedly entering your credentials with the browser and modern connection methods.
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
Modern authentication with -ModernAuth is the most secure and persistent authentication method, and it can be used with Username and Password variables as long as Multi Factor Authentication (MFA) is not enforced. There are also a few limitations. See Connect-Site to learn different ways to use this connection method, or to switch to Browser authentication if needed.
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, see the Incremental update using PowerShell article.
To replicate the copy options from ShareGate Migrate, see Replicate copy options.
If you have hundreds of gigabytes of data to migrate, we recommend creating multiple CSVs to run the migration in smaller batches.
Script Errors and Testing: Test your PowerShell script on a small subset of OneDrive accounts before applying it broadly. Testing helps identify potential issues in a controlled scope.

