![]()
Before migrating SharePoint or OneDrive data, it is essential to clean up unused files, outdated user data, orphaned sites, and unnecessary permissions. This ensures a smooth migration, optimized storage, and minimal post-migration issues.
In this guide, we will cover:
- Identifying inactive users and their data.
- Cleaning up unused files and folders.
- Removing orphaned OneDrive sites.
- Archiving important documents.
- Automating cleanup tasks with PnP PowerShell.
Step 1: Connect to SharePoint Online
First, establish a secure connection to SharePoint Online using PnP PowerShell.
$adminSiteUrl = "https://yourtenant-admin.sharepoint.com"
Connect-PnPOnline -Url $adminSiteUrl -Interactive
✔ Ensures authentication to manage user data across sites.
Step 2: Identify Inactive Users and Their Data
Find inactive users who haven’t accessed SharePoint/OneDrive in the last 6 months.
$inactiveUsers = Get-PnPUserProfileProperty -All | Where-Object { $_.LastLogonTime -lt (Get-Date).AddMonths(-6) }
$inactiveUsers | Select DisplayName, Email, LastLogonTime
✔ Lists inactive users with last login details.
Step 3: List and Remove Unused Files
Find large files (over 500MB) and old files (not modified in 1 year).
$oldFiles = Get-PnPListItem -List "Documents" | Where-Object { $_["File_x0020_Size"] -gt 500000 -or $_["Modified"] -lt (Get-Date).AddYears(-1) }
$oldFiles | Select FileRef, File_x0020_Size, Modified
✔ Identifies large/old files for cleanup.
To delete them:
foreach ($file in $oldFiles) {
Remove-PnPListItem -List "Documents" -Identity $file.Id -Recycle
Write-Host "Deleted: $($file.FileRef)"
}
✔ Moves files to Recycle Bin for safety.
Step 4: Remove Orphaned OneDrive Sites
Find and remove orphaned OneDrive sites (owners who left the organization).
$orphanedSites = Get-PnPTenantSite -IncludeOneDriveSites | Where-Object { $_.Status -eq "Orphaned" }
$orphanedSites | Select Url, Owner, StorageUsage
✔ Identifies orphaned OneDrive sites.
To delete them:
foreach ($site in $orphanedSites) {
Remove-PnPTenantSite -Url $site.Url -Force
Write-Host "Deleted orphaned OneDrive: $($site.Url)"
}
✔ Frees up unused storage.
Step 5: Archive Important Documents Before Deletion
Move important documents to an archive library.
$archiveLibrary = "Archived Documents"
foreach ($file in $oldFiles) {
Move-PnPFile -ServerRelativeUrl $file.FileRef -TargetLibrary $archiveLibrary
Write-Host "Archived: $($file.FileRef)"
}
✔ Ensures critical files are preserved.
Step 6: Remove Unused SharePoint Groups and Permissions
List inactive SharePoint groups (unused for 6+ months).
$groups = Get-PnPGroup | Where-Object { $_.Owner -eq $null -or $_.LastUpdated -lt (Get-Date).AddMonths(-6) }
$groups | Select Title, Owner, LastUpdated
✔ Identifies inactive SharePoint groups.
To delete them:
foreach ($group in $groups) {
Remove-PnPGroup -Identity $group.Title
Write-Host "Removed group: $($group.Title)"
}
✔ Cleans up unused permissions.
Step 7: Automate the Cleanup Process for Multiple Sites
Run cleanup across all site collections before migration.
$allSites = Get-PnPTenantSite | Where-Object { $_.Template -ne "RedirectSite" }
foreach ($site in $allSites) {
Connect-PnPOnline -Url $site.Url -Interactive
Write-Host "Cleaning up: $($site.Url)"
# Remove old files
$oldFiles = Get-PnPListItem -List "Documents" | Where-Object { $_["Modified"] -lt (Get-Date).AddYears(-1) }
foreach ($file in $oldFiles) {
Remove-PnPListItem -List "Documents" -Identity $file.Id -Recycle
}
# Remove orphaned groups
$groups = Get-PnPGroup | Where-Object { $_.Owner -eq $null -or $_.LastUpdated -lt (Get-Date).AddMonths(-6) }
foreach ($group in $groups) {
Remove-PnPGroup -Identity $group.Title
}
Write-Host "Cleanup completed for: $($site.Url)"
}
✔ Automates cleanup across all SharePoint sites.
