Powershell Upload File to Onedrive for Business
Transfer all OneDrive files to some other user via PowerShell
During the offboarding of an Office 365 user, you lot may be required to make a re-create of their OneDrive files or transfer buying of the files to someone else. This tin be a laborious process, requiring you lot to log into the departing users OneDrive and downloading, transferring or sharing their data manually.
The expert news is, we tin do this with PowerShell. The below script makes it relatively easy to copy a user'southward entire OneDrive directory into a subfolder in some other user'southward OneDrive.
The bad news is, at that place are a few things yous'll demand to consider earlier you utilise information technology:
Things to go along in mind
- This could take a while.
Each file or binder 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, y'all may want to utilise another method. - It uses your connection
It downloads the files before uploading them to the other user's OneDrive. If your connection is boring, and y'all're moving large files you might want to leave this running on a cloud hosted server. - Information technology can't move files larger than 250MB
A limitation of this PowerShell module is that it tin't send files larger than 250MB to SharePoint. This script will make a note of these files and consign a listing of them to c:\temp\largefiles.txt in instance you desire to move them manually. - No Two Factor Authentication
This script doesn't work with multifactor authentication on the admin account. You may desire to create a temporary admin without MFA for this purpose.
Prerequisites
For this script to work, y'all'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 information technology here: https://world wide web.microsoft.com/en-au/download/details.aspx?id=35588
SharePoint PnP Powershell Module
The SharePoint PnP Powershell module provides the cmdlets we'll apply to transfer the files and folders betwixt OneDrive accounts.
To install it, open a PowerShell window equally an administrator and run the following cmdlet:
Install-Module SharePointPnPPowerShellOnline -Force
MSOnline V1 Powershell Module
You'll too 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
How to copy OneDrive files betwixt users via PowerShell
- Open up Visual Studio Code, or PowerShell ISE and re-create and paste the script at the bottom of this article.
- Run it past pressing F5
- Follow the prompts, entering the post-obit info:
The username of the departing user. This is the user whose OneDrive we'll be copying
The username of the destination user. This is the user that volition receive the OneDrive files in a subfolder within their OneDrive
The username of your Office 365 Admin.
- The script will bank check for files too large to be transferred. If there are whatever, their details will be logged in C:\temp\largefiles.txt
- Look for the folders and files to be created. Folders are created beginning, so that the Re-create-PnPFile cmdlet has an existing path to identify the files.
- Once information technology's washed, you'll observe the files and folders in the destination users OneDrive under a subfolder called "Departing User" files. Where Departing User is the brandish name of the user that'due south leaving.
Consummate PowerShell script to transfer OneDrive information to some other user
$departinguser = Read-Host "Enter departing user's e-mail" $destinationuser = Read-Host "Enter destination user'due south e-mail" $globaladmin = Read-Host "Enter the username of your Global Admin account" $credentials = Go-Credential -Credential $globaladmin Connect-MsolService -Credential $credentials $InitialDomain = Get-MsolDomain | Where-Object {$_.IsInitial -eq $true} $SharePointAdminURL = "https://$($InitialDomain.Name.Split(".")[0])-admin.sharepoint.com" $departingUserUnderscore = $departinguser -replace "[^a-zA-Z]", "_" $destinationUserUnderscore = $destinationuser -replace "[^a-zA-Z]", "_" $departingOneDriveSite = "https://$($InitialDomain.Name.Split(".")[0])-my.sharepoint.com/personal/$departingUserUnderscore" $destinationOneDriveSite = "https://$($InitialDomain.Proper noun.Split(".")[0])-my.sharepoint.com/personal/$destinationUserUnderscore" Write-Host "`nConnecting to SharePoint Online" -ForegroundColor Blueish Connect-SPOService -Url $SharePointAdminURL -Credential $credentials Write-Host "`nAdding $globaladmin as site collection admin on both OneDrive site collections" -ForegroundColor Bluish # 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'due south OneDrive via SharePoint Online PNP module" -ForegroundColor Blueish Connect-PnPOnline -Url $departingOneDriveSite -Credentials $credentials Write-Host "`nGetting display name of $departinguser" -ForegroundColor Bluish # Get name of departing user to create folder proper name. $departingOwner = Get-PnPSiteCollectionAdmin | Where-Object {$_.loginname -lucifer $departinguser} # If in that location's an issue retrieving the departing user'due south display proper name, set this 1. if ($departingOwner -contains $null) { $departingOwner = @{ Title = "Parting 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.Championship)" -ForegroundColor Blue # Get all items from source OneDrive $items = Get-PnPListItem -List Documents -PageSize k $largeItems = $items | Where-Object {[long]$_.fieldvalues.SMTotalFileStreamSize -ge 261095424 -and $_.FileSystemObjectType -contains "File"} if ($largeItems) { $largeexport = @() foreach ($item in $largeitems) { $largeexport += "$(Get-Engagement) - Size: $([math]::Round(($item.FieldValues.SMTotalFileStreamSize / 1MB),2)) MB Path: $($item.FieldValues.FileRef)" Write-Host "File too large to copy: $($particular.FieldValues.FileRef)" -ForegroundColor DarkYellow } $largeexport | Out-file C:\temp\largefiles.txt -Append Write-Host "A listing of files too large to be copied from $($departingOwner.Championship) have been exported to C:\temp\LargeFiles.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 past Folders to create directory construction $folders = $rightSizeItems | Where-Object {$_.FileSystemObjectType -contains "Binder"} Write-Host "`nCreating Directory Construction" -ForegroundColor Bluish foreach ($folder in $folders) { $path = ('{0}{1}' -f $destinationOneDriveSiteRelativePath, $folder.fieldvalues.FileRef).Supplant($departingOneDrivePath, '') Write-Host "Creating folder in $path" -ForegroundColor Greenish $newfolder = Ensure-PnPFolder -SiteRelativePath $path } Write-Host "`nCopying Files" -ForegroundColor Blueish $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 Drove Admin role for both users Write-Host "`nRemoving $globaladmin from OneDrive site collections" -ForegroundColor Blue Set up-SPOUser -Site $departingOneDriveSite -LoginName $globaladmin -IsSiteCollectionAdmin $false Set-SPOUser -Site $destinationOneDriveSite -LoginName $globaladmin -IsSiteCollectionAdmin $false Write-Host "`nComplete!" -ForegroundColor Green Source: https://gcits.com/knowledge-base/transfer-users-onedrive-files-another-user-via-powershell/
0 Response to "Powershell Upload File to Onedrive for Business"
Post a Comment