Office 365: Migrating files from a OneDrive account to another using PowerShell
[pl_row] [pl_col col=12] [pl_text]

During the off-boarding of an Office 365 user, you may be required to make a copy of their OneDrive files or transfer ownership of the files to someone else. This can be a laborious process, requiring you to log into the departing users OneDrive and downloading, transferring or sharing their data manually.

The good news is, we can do this with PowerShell. The attached script makes it relatively easy to copy a user’s entire OneDrive directory into a sub-folder in another user’s OneDrive.

There are a few things to keep in mind:

  • This could take a while.
  •  Each file or folder takes at least a second or two to process. If your departing user has tens of thousands of files, and time is not on your side, you may want to use another method.
  • It uses your connection
  •  It downloads the files before uploading them to the other user’s OneDrive. If your connection is slow, and you’re moving large files you might want to leave this running on a cloud-hosted server.
  • It can’t move files larger than 250 MB
  •  A limitation of this PowerShell module is that it can’t send files larger than 250 MB to SharePoint. This script will make a note of these files and export a list of them to c:templargefiles.txt in case you want to move them manually.
  • No Two Factor Authentication
  •  This script doesn’t work with multi-factor authentication on the admin account. You may want to create a temporary admin without MFA for this purpose.


For this script to work, you’ll need to install the following PowerShell Modules:

SharePoint Online Management Shell

The SharePoint Online Management Shell is used to modify the permissions on the users’ OneDrive site collections.

Download and install it here:

SharePoint PnP Powershell Module

The SharePoint PnP PowerShell module provides the cmdlets we’ll use to transfer the files and folders between OneDrive accounts.

To install it, open a PowerShell window as an administrator and run the following cmdlet:

Install-Module SharePointPnPPowerShellOnline -Force

MSOnline V1 Powershell Module

You’ll also need the MSOnline V1 PowerShell Module for this script.

To install it, open a PowerShell window as an administrator and run the following cmdlet:

  Install-Module MSOnline -Force

Find below the PnP Script that will achieve the desired Process;

$departinguser = Read-Host “Enter departing user’s email”

$destinationuser = Read-Host “Enter destination user’s email”

$globaladmin = Read-Host “Enter the username of your Global Admin account”

$credentials = Get-Credential -Credential $globaladmin

Connect-MsolService -Credential $credentials

$InitialDomain = Get-MsolDomain | Where-Object {$_.IsInitial -eq $true}

$SharePointAdminURL = “https://$($InitialDomain.Name.Split(“.”)[0])”

$departingUserUnderscore = $departinguser -replace “[^a-zA-Z]”, “_”

$destinationUserUnderscore = $destinationuser -replace “[^a-zA-Z]”, “_”

$departingOneDriveSite = “https://$($InitialDomain.Name.Split(“.”)[0])$departingUserUnderscore”

$destinationOneDriveSite = “https://$($InitialDomain.Name.Split(“.”)[0])$destinationUserUnderscore”

Write-Host “`nConnecting to SharePoint Online” -ForegroundColor Blue

Connect-SPOService -Url $SharePointAdminURL -Credential $credentials

Write-Host “`nAdding $globaladmin as site collection admin on both OneDrive site collections” -ForegroundColor Blue

# Set current admin as a Site Collection Admin on both OneDrive Site Collections

Set-SPOUser -Site $departingOneDriveSite -LoginName $globaladmin -IsSiteCollectionAdmin $true

Set-SPOUser -Site $destinationOneDriveSite -LoginName $globaladmin -IsSiteCollectionAdmin $true

Write-Host “`nConnecting to $departinguser’s OneDrive via SharePoint Online PNP module” -ForegroundColor Blue

Connect-PnPOnline -Url $departingOneDriveSite -Credentials $credentials

Write-Host “`nGetting display name of $departinguser” -ForegroundColor Blue

# Get name of departing user to create folder name.

$departingOwner = Get-PnPSiteCollectionAdmin | Where-Object {$_.loginname -match $departinguser}

# If there’s an issue retrieving the departing user’s display name, set this one.

if ($departingOwner -contains $null) {

    $departingOwner = @{

        Title = “Departing User”



# Define relative folder locations for OneDrive source and destination

$departingOneDrivePath = “/personal/$departingUserUnderscore/Documents”

$destinationOneDrivePath = “/personal/$destinationUserUnderscore/Documents/$($departingOwner.Title)’s Files”

$destinationOneDriveSiteRelativePath = “Documents/$($departingOwner.Title)’s Files”

Write-Host “`nGetting all items from $($departingOwner.Title)” -ForegroundColor Blue

# Get all items from source OneDrive

$items = Get-PnPListItem -List Documents -PageSize 1000

$largeItems = $items | Where-Object {[long]$_.fieldvalues.SMTotalFileStreamSize -ge 261095424 -and $_.FileSystemObjectType -contains “File”}

if ($largeItems) {

    $largeexport = @()

    foreach ($item in $largeitems) {

        $largeexport += “$(Get-Date) – Size: $([math]::Round(($item.FieldValues.SMTotalFileStreamSize / 1MB),2)) MB Path: $($item.FieldValues.FileRef)”

        Write-Host “File too large to copy: $($item.FieldValues.FileRef)” -ForegroundColor DarkYellow


    $largeexport | Out-file C:templargefiles.txt -Append

    Write-Host “A list of files too large to be copied from $($departingOwner.Title) have been exported to C:tempLargeFiles.txt” -ForegroundColor Yellow


$rightSizeItems = $items | Where-Object {[long]$_.fieldvalues.SMTotalFileStreamSize -lt 261095424 -or $_.FileSystemObjectType -contains “Folder”}

Write-Host “`nConnecting to $destinationuser via SharePoint PNP PowerShell module” -ForegroundColor Blue

Connect-PnPOnline -Url $destinationOneDriveSite -Credentials $credentials

Write-Host “`nFilter by folders” -ForegroundColor Blue

# Filter by Folders to create directory structure

$folders = $rightSizeItems | Where-Object {$_.FileSystemObjectType -contains “Folder”}

Write-Host “`nCreating Directory Structure” -ForegroundColor Blue

foreach ($folder in $folders) {

    $path = (‘{0}{1}’ -f $destinationOneDriveSiteRelativePath, $folder.fieldvalues.FileRef).Replace($departingOneDrivePath, ”)

    Write-Host “Creating folder in $path” -ForegroundColor Green

    $newfolder = Ensure-PnPFolder -SiteRelativePath $path


Write-Host “`nCopying Files” -ForegroundColor Blue

$files = $rightSizeItems | Where-Object {$_.FileSystemObjectType -contains “File”}

$fileerrors = “”

foreach ($file in $files) {

    $destpath = (“$destinationOneDrivePath$($file.fieldvalues.FileDirRef)”).Replace($departingOneDrivePath, “”)

    Write-Host “Copying $($file.fieldvalues.FileLeafRef) to $destpath” -ForegroundColor Green

    $newfile = Copy-PnPFile -SourceUrl $file.fieldvalues.FileRef -TargetUrl $destpath -OverwriteIfAlreadyExists -Force -ErrorVariable errors -ErrorAction SilentlyContinue

    $fileerrors += $errors


$fileerrors | Out-File c:tempfileerrors.txt

# Remove Global Admin from Site Collection Admin role for both users

Write-Host “`nRemoving $globaladmin from OneDrive site collections” -ForegroundColor Blue

Set-SPOUser -Site $departingOneDriveSite -LoginName $globaladmin -IsSiteCollectionAdmin $false

Set-SPOUser -Site $destinationOneDriveSite -LoginName $globaladmin -IsSiteCollectionAdmin $false

Write-Host “`nComplete!” -ForegroundColor Green

Feel free to drop your comments and feedback

[/pl_text] [/pl_col] [/pl_row]

4 thoughts on “Office 365: Migrating files from a OneDrive account to another using PowerShell

  1. Chris says:

    Hello. This is extremely useful. Is there a way to edit the script so it moves all of the files instead of copying them?

    1. Sure, You can initiate a Move Operation instead but that is not advisable as the copy operation will give us a failsafe should the whole process not complete eventually

  2. David says:

    Script works great expect for one issue. After it creates the folders in the destination OneDrive, it then shows files being copied to the destination, however, those files are not copied.

    I am not seeing any errors during the process nor are any errors being logged.

    Any idea what might be happening?

    1. Do you get any error log from the log files generated?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may also like