Orphaned OneDrive for Business sites are created when an employee leaves an organization, and their OneDrive account remains active without an owner. These orphaned sites consume storage and may pose security risks if not managed properly.
With PnP PowerShell, you can automate the detection and cleanup of orphaned OneDrive sites by:
✔ Identifying sites without an active owner
✔ Notifying administrators for review
✔ Deleting orphaned sites automatically
Step 1: Install & Update PnP PowerShell
Before running any script, ensure PnP PowerShell is installed. Open PowerShell as Administrator and run:
Install-Module -Name PnP.PowerShell -Force -AllowClobber
To update:
Update-Module -Name PnP.PowerShell
Step 2: Connect to SharePoint Admin Center
Since OneDrive for Business is part of SharePoint Online, connect to the SharePoint Admin Center:
$adminUrl = "https://yourtenant-admin.sharepoint.com"
Connect-PnPOnline -Url $adminUrl -Scopes "Sites.FullControl.All" -Interactive
For app-based authentication:
$clientId = "your-client-id"
$tenantId = "your-tenant-id"
$clientSecret = "your-client-secret"
Connect-PnPOnline -Url $adminUrl -ClientId $clientId -ClientSecret $clientSecret -Tenant $tenantId
Step 3: Retrieve All OneDrive Sites
To list all OneDrive sites in your tenant:
$oneDriveSites = Get-PnPTenantSite -IncludeOneDriveSites | Where-Object { $_.Url -like "*-my.sharepoint.com/personal/*" }
$oneDriveSites
Step 4: Identify Orphaned OneDrive Sites
Find OneDrive Sites Without Owners
To identify orphaned OneDrive sites, check if the SiteOwner field is empty:
$orphanedSites = @()
foreach ($site in $oneDriveSites) {
if (-not $site.Owner) {
$orphanedSites += [PSCustomObject]@{
OneDriveSite = $site.Url
LastActivity = $site.LastContentModifiedDate
}
}
}
$orphanedSites | Format-Table -AutoSize
Step 5: Export Orphaned Sites Report
To export orphaned sites to a CSV report for manual review:
$reportPath = "C:\Reports\Orphaned_OneDrive_Sites.csv"
$orphanedSites | Export-Csv -Path $reportPath -NoTypeInformation
Write-Host "Orphaned OneDrive sites report exported to $reportPath"
Step 6: Notify Admins About Orphaned Sites
To send an email to IT admins for reviewing orphaned sites, use the Microsoft Graph API:
$adminEmail = "admin@yourdomain.com"
$subject = "Orphaned OneDrive Sites Report"
$body = "Please review the attached report of orphaned OneDrive sites."
Send-MailMessage -To $adminEmail -From "noreply@yourdomain.com" -Subject $subject -Body $body -Attachments $reportPath -SmtpServer "smtp.office365.com" -UseSsl -Port 587 -Credential (Get-Credential)
Step 7: Automatically Delete Orphaned OneDrive Sites
Once orphaned sites are identified, they can be deleted automatically:
foreach ($site in $orphanedSites) {
Write-Host "Deleting orphaned OneDrive site: $($site.OneDriveSite)"
Remove-PnPTenantSite -Url $site.OneDriveSite -Force
}
Step 8: Automate the Cleanup Process
1. Open Task Scheduler
- Click Start, search for Task Scheduler, and open it.
- Click Create Basic Task.
- Name it “Orphaned OneDrive Cleanup”.
2. Set Trigger
- Choose Weekly or another frequency.
- Set execution time.
3. Set Action
- Select Start a Program.
- In Program/Script, enter:
powershell.exe
- In Arguments, enter:
-File "C:\Scripts\OrphanedOneDriveCleanup.ps1"
- Click Finish.
This will detect and delete orphaned OneDrive sites automatically.
Step 9: Monitor & Validate Cleanup
To manually check if the orphaned OneDrive sites were deleted, run:
Get-PnPTenantSite -IncludeOneDriveSites | Where-Object { $_.Url -like "*-my.sharepoint.com/personal/*" }
✔ Ensures only active sites remain
✔ Confirms successful cleanup