Skip to main content

Import Google Drive documents to OneDrive for Business with PowerShell

Updated over 2 weeks ago

This guide explains how to use PowerShell to automate the migration of files from Google My Drive to OneDrive for Business using the Import-GoogleDriveDocument command.

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.

Tip: This guide focuses on migrating your files and folders to the root of the Documents library in OneDrive. To migrate your documents to a new folder, refer to the Walkthrough - Migrate to a new OneDrive folder in PowerShell.

Prerequisites

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

How-to

Create a CSV guide for your migration

To automate the migration, you first need to create a CSV file that maps your Google Drives to your OneDrives:

  1. Download a list of all your Google Drive user emails as a CSV.

  2. Open the CSV file.

  3. Edit the file to keep only one column with the Google Drive email addresses.

  4. Insert a header row.

  5. Name the first column GOOGLEDRIVEEMAIL.

  6. Open ShareGate Migrate.

  7. Select All reports from the sidebar.

  8. Click Create custom report.

  9. Select OneDrive for Business as your object type.

  10. Click Continue without saving.

  11. Select your tenant.

  12. Click Next.

  13. Toggle off Use local cache.

  14. Click Run now.

  15. Select all OneDrive for Business accounts using the checkmark at the top of the list.

  16. Click Edit in the Quick actions menu.

  17. Select Add administrators in the Transformations dropdown.

  18. Add your account in the Select user or group field that appears.

  19. Click Apply.

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

  21. Click Export in the top right corner.

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

  23. Replace the Site address header with ONEDRIVEURL.

  24. Combine this CSV with the Google Drive email CSV so that each Google Drive email matches the correct OneDrive for Business URL.


    Note: If sorting alphabetically doesn't work, you can reorganize your data manually or use Excel macros or PowerShell to find a solution.

  25. Save this new file as a CSV.

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 ","

# Connect to Google Drive as an admin
$connection = Connect-GoogleDrive -Email [email protected] -Admin

# Define OneDrive 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 user's OneDrive site
$dstSite = Connect-Site -Url $row.ONEDRIVEURL -UseCredentialsFrom $dstSiteConnection

# Get the "Documents" list from the OneDrive site
$dstList = Get-List -Name Documents -Site $dstSite

# Get the Google Drive for the specified user
$myDrive = Get-GoogleMyDrive -Connection $connection -Email $row.GOOGLEDRIVEEMAIL

# Import documents from Google Drive to the OneDrive "Documents" list
Import-GoogleDriveDocument -Drive $myDrive -SourceView "MyDocuments" -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 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 to open the CSV in Notepad).

  • $connection: Replace the email with your Google Drive admin email. For more information, see Connect Google Drive.

  • $dstSiteConnection: Replace the URL with the root URL of your OneDrives in the corresponding tenant.

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

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

  • 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. To learn more about how to use foreach in your ShareGate PowerShell scripts, see Copy to and from multiple destinations with a foreach loop statement and a CSV file in PowerShell.

  • -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 tenant(s), 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.

  • Remove-SiteCollectionAdministrator: Removes your user account as site collection administrator on the OneDrives after each Google Drive 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 for Microsoft 365, 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 can find them in Tasks. You can also export them with Export-Report in your script.

  • You can schedule your migration using PowerShell to run it off-hours and optimize performance.

  • To learn more about using copy options in PowerShell, see Copy options in PowerShell.

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

Did this answer your question?