Transfer OneDrive files to another user via PowerShell

Channel:
Subscribers:
1,660
Published on ● Video Link: https://www.youtube.com/watch?v=oAF_EwSZydI



Duration: 3:14
20 views
0


Here's how to transfer OneDrive files to another user via PowerShell. See the original script codes@ https://www.reddit.com/r/windows/comments/1398ygv/script_for_transferring_onedrive_files_to_another/.

To transfer OneDrive files to another user via PowerShell, you need to install some PowerShell modules and run a script that will copy the files and folders from one account to another.

The PowerShell modules you need are:

- **SharePoint Online Management Shell**: This is used to modify the permissions on the users' OneDrive site collections. You can download and install it from here: https://www.microsoft.com/en-au/download/details.aspx?id=35588
- **SharePoint PnP PowerShell Module**: This provides the cmdlets to transfer the files and folders between OneDrive accounts. You can install it by running this command in PowerShell as an admin: `Install-Module SharePointPnPPowerShellOnline -Force`
- **MSOnline V1 PowerShell Module**: This is needed for the script to work. You can install it by running this command in PowerShell as an admin: `Install-Module MSOnline -Force`

The script you need to run is provided at the top of this video description. You can copy and paste it into Visual Studio Code or PowerShell ISE and run it by pressing F5. You will be prompted to enter the following information:

- The username of the departing user. This is the user whose OneDrive you want to copy.
- The username of the destination user. This is the user that will receive the OneDrive files in a subfolder within their OneDrive.
- The username of your Office 365 Admin.

The script will then download the files from the departing user's OneDrive and upload them to the destination user's OneDrive. It will also create a log file in c:\temp\onedrivecopy.log that will show you the progress and any errors.

Please note that this script has some limitations:

- It may take a long time depending on the number and size of the files.
- It uses your internet connection to download and upload the files, so make sure you have enough bandwidth and data allowance.
- It cannot move files larger than 250MB. It will make a note of these files and export a list of them to c:\temp\largefiles.txt in case you want to move them manually.
- It does not work with multifactor authentication on the admin account. You may want to create a temporary admin without MFA for this purpose.

Plan B: you may also transfer OneDrive files to another user via PowerShell. Here are the steps:

1. Open PowerShell.
2. Import the SharePoint Online Client SDK.
3. Get the current user.
4. Get the destination user.
5. Get the list of files to transfer.
6. Create a new folder in the destination user's OneDrive for Business.
7. Copy the files to the new folder.
8. Delete the files from the current user's OneDrive for Business.

Here is the PowerShell code:

```
# Import the SharePoint Online Client SDK
Import-Module Microsoft.SharePoint.Client

# Get the current user
$currentUser = Get-SPOUser -Identity $env:USERNAME

# Get the destination user
$destinationUser = Get-SPOUser -Identity "username@tenant.onmicrosoft.com"

# Get the list of files to transfer
$files = Get-ChildItem -Path "C:\path\to\files"

# Create a new folder in the destination user's OneDrive for Business
$newFolder = $destinationUser.Personal.CreateFolder("New Folder")

# Copy the files to the new folder
foreach ($file in $files) {
Copy-Item $file.FullName $newFolder.Path
}

# Delete the files from the current user's OneDrive for Business
foreach ($file in $files) {
Remove-Item $file.FullName
}
```

You can also use the SharePoint Online PowerShell cmdlets to transfer OneDrive files to another user. Here are the steps:

1. Open PowerShell.
2. Import the SharePoint Online PowerShell module.
3. Connect to SharePoint Online.
4. Get the current user.
5. Get the destination user.
6. Get the list of files to transfer.
7. Create a new folder in the destination user's OneDrive for Business.
8. Copy the files to the new folder.
9. Disconnect from SharePoint Online.

Here is the PowerShell code:

```
# Import the SharePoint Online PowerShell module
Import-Module 'SharePointOnline'

# Connect to SharePoint Online
Connect-PnPOnline -Url https://contoso.sharepoint.com

# Get the current user
$currentUser = Get-PnPUser -Identity $env:USERNAME

# Get the destination user
$destinationUser = Get-PnPUser -Identity "username@tenant.onmicrosoft.com"

# Get the list of files to transfer
$files = Get-ChildItem -Path "C:\path\to\files"

# Create a new folder in the destination user's OneDrive for Business
$newFolder = $destinationUser.Personal.CreateFolder("New Folder")

# Copy the files to the new folder
foreach ($file in $files) {
Copy-PnPFile -Path $file.FullName -Destination $newFolder.Path
}

# Disconnect from SharePoint Online
Disconnect-PnPOnline
```