Saturday, June 3, 2023
HomeWINDOWSHow to transfer OneDrive files to another user via PowerShell

How to transfer OneDrive files to another user via PowerShell


Transferring files from your Microsoft OneDrive account to another user is easy in the sense that you can download the content from your OneDrive, then manually upload them to the other account. In this post, we will show you how to transfer OneDrive files to another user via PowerShell.

How to transfer OneDrive files to another user via PowerShell

Things to consider

When it comes down to uploading files from your OneDrive to another account, it’s a task that will take some time because it’s not possible at this time to upload files larger than 250MB. The good news is PowerShell will make a note of all files it cannot upload, so you can seek them out and share them via the regular method.

Before uploading the files to the other OneDrive account, the files will first be downloaded to your computer, so ensure you have enough space on your hard drive or SSD before moving forward. And since your internet connection is required, the overall speed of transfer will depend on the quality of the network.

Now, we must note that two-factor authentication does not exist on the administrator account, so create a temporary admin account that doesn’t have 2FA for this purpose only.

Things you will need

We will use a special script to move the files from one OneDrive account to another. So, for the script to work with problems, please install the following PowerShell Modules right now:

SharePoint PnP PowerShell Module

Open the PowerShell tool as an admin, then run the following command:

Install-Module SharePointPnPPowerShellOnline -Force

SharePoint Online Management Shell

The purpose of this tool is to modify the permissions on the users’ OneDrive account.

Download and install it for free from

MSOnline V1 Powershell Module

In order to install this final Module, please run the following command in PowerShell as an admin:

Install-Module MSOnline -Force

How to transfer OneDrive files to another account

To transfer files from your OneDrive account to another, you have to open PowerShell and then run the provided script.

Open PowerShell

Microsoft PowerShell Search

Open Visual Studio Code, or PowerShell.

You can do this by clicking on the Search button, then search for PowerShell.

From there, right-click on the app, then select the option designed to open the tool in Admin mode.

Run the script

OneDrive PowerShell Script

Next, you must run the relevant script. You can find it at the bottom of the article.

We chose to do this because the script is quite long.

After adding the script, hit the Enter key on your keyboard.

Transfer the files

Finally, it’s now time to transfer the files to another OneDrive account.

You see, right after hitting the Enter key, you will be asked to add the email account The username of the departing user.

You will also need The username of the destination user. This is the OneDrive user where the files will be copied and transferred to.

Finally, you’ll be asked to add The username of your Office 365 Admin.

Wait for the script to do its thing before checking the receiving account to see if the files were transferred correctly.

Copy and paste the below script:

$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



$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 = ('01' -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:\temp\fileerrors.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

You can find the script on this Reddit page.

READHow to export CSV in PowerShell on Windows

Can PowerShell access OneDrive?

SharePoint Online PowerShell will make it possible for users to connect to another OneDrive account using the PowerShell tool. It will ask you to insert your password in order for PowerShell to begin working on your OneDrive accounts by means of cmdlets.

Can OneDrive be accessed by external users?

Your OneDrive account can be accessed by external users, but only if you allow it. Users can have access to your files forever or for a set time period. You can also limit what they can do.

How to copy files from another person’s OneDrive?

You have the following options if you want o copy files from another person’s OneDrive:

  • Open OneDrive in your browser using the link, select the files you want to copy and click Download. This will download it to your computer.
  • Open the OneDrive account using the link, select the files you want to copy, and click Copy to.

That’s it!

How to transfer OneDrive files to another user via PowerShell

Source link


Please enter your comment!
Please enter your name here

Most Popular