Transferring Ownership of OneDrive Accounts using PnP PowerShell

Loading

When an employee leaves an organization, their OneDrive for Business data needs to be transferred to a new owner. Microsoft 365 allows administrators to assign new ownership of a user’s OneDrive before account deletion. PnP PowerShell simplifies this process, ensuring a seamless transition of files.

This guide will cover:
Identifying the OneDrive URL of the departing user
Granting access to a new owner
Transferring ownership and permissions
Automating the process using PowerShell scripts


Prerequisites

1️⃣ Install PnP PowerShell

Ensure PnP PowerShell is installed on your system:

Install-Module -Name PnP.PowerShell -Force -AllowClobber

2️⃣ Connect to SharePoint Admin Center

Connect-PnPOnline -Url "https://yourtenant-admin.sharepoint.com" -Interactive

Replace "yourtenant" with your actual SharePoint tenant name.

Use Global Administrator or SharePoint Administrator credentials.


Step 1: Get the OneDrive URL of the Departing User

Each user’s OneDrive URL follows this format:

https://yourtenant-my.sharepoint.com/personal/username_domain_com

You can retrieve it using:

# Define user details
$UserPrincipalName = "olduser@yourtenant.com"

# Get OneDrive URL for the user
$OneDriveURL = Get-PnPUserProfileProperty -Account $UserPrincipalName | Select-Object PersonalUrl

Write-Host "OneDrive URL: $OneDriveURL"

Fetches the OneDrive link of the departing user.


Step 2: Grant the New Owner Access to OneDrive

Before transferring ownership, assign the new owner Full Control:

# Define variables
$OldUser = "olduser@yourtenant.com"
$NewOwner = "newuser@yourtenant.com"

# Get OneDrive URL
$OneDriveURL = Get-PnPUserProfileProperty -Account $OldUser | Select-Object PersonalUrl

# Connect to the departing user's OneDrive
Connect-PnPOnline -Url $OneDriveURL -Interactive

# Grant Full Control
Set-PnPListItemPermission -List "Documents" -Identity 1 -User $NewOwner -AddRole 'Full Control'

Write-Host "Access granted to $NewOwner"

The new owner can now access all files in OneDrive.


Step 3: Transfer Ownership of OneDrive

To officially change ownership:

# Define variables
$OldUser = "olduser@yourtenant.com"
$NewOwner = "newuser@yourtenant.com"

# Assign the new user as the site collection administrator
Set-PnPTenantSite -Url $OneDriveURL -Owner $NewOwner

Write-Host "Ownership transferred to $NewOwner"

The new owner becomes the OneDrive admin.


Step 4: Automating the Transfer for Multiple Users

If multiple users are leaving, automate the process:

# Import CSV file with old and new owners
$Users = Import-Csv "C:\OneDriveTransfers.csv"

foreach ($User in $Users) {
$OldUser = $User.OldOwner
$NewOwner = $User.NewOwner

# Get OneDrive URL
$OneDriveURL = Get-PnPUserProfileProperty -Account $OldUser | Select-Object PersonalUrl

# Connect to OneDrive
Connect-PnPOnline -Url $OneDriveURL -Interactive

# Transfer ownership
Set-PnPTenantSite -Url $OneDriveURL -Owner $NewOwner

Write-Host "Ownership transferred from $OldUser to $NewOwner"
}

Bulk transfers ownership using a CSV file.

CSV Format Example:

OldOwner,NewOwner
user1@yourtenant.com,newuser1@yourtenant.com
user2@yourtenant.com,newuser2@yourtenant.com

Step 5: Verify New Ownership

After transferring ownership, confirm the new owner:

# Verify the new OneDrive site owner
Get-PnPTenantSite -Url $OneDriveURL | Select-Object Owner

Displays the current OneDrive owner.


Step 6: Remove Old Owner’s Access

Once ownership is transferred, remove the old user’s access:

# Remove old owner's permissions
Remove-PnPUser -LoginName $OldUser -List "Documents"

Write-Host "Removed $OldUser from OneDrive"

Ensures the old owner no longer has access.


Step 7: Automating the Process with Task Scheduler

Schedule the script to run automatically using Task Scheduler:

1️⃣ Open Task Scheduler (taskschd.msc).
2️⃣ Click Create Basic Task → Name it OneDrive Ownership Transfer.
3️⃣ Set Trigger to Weekly or Monthly.
4️⃣ Choose ActionStart a Program.
5️⃣ Select PowerShell (C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe).
6️⃣ Add script path in Arguments:

-ExecutionPolicy Bypass -File "C:\Scripts\OneDriveTransfer.ps1"

7️⃣ Click Finish.

Now OneDrive ownership transfers will be automated!

Leave a Reply

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