Remove Orphans objects in SharePoint using PowerShell
Using SharePoint there can appear orphans objects. This is when there must be an object – there are records about it in database, but there’s no this object. It can be a result of incorrect deletion or troubles with network while working.
Until you are going to install updates or prepare to migration, you can not even think about them. But in case of updating or migration you should fix these objects. Here’s a PowerShell script to do it. It checks and fixes in all content databases.
if ((Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{Add-PSSnapin Microsoft.SharePoint.Powershell}
$CDBs = Get-SPContentDatabase
ForEach ($CDB in $CDBs)
{
Write-Host "Detecting Orphans for " $CDB.Name
$CDB.Repair($false)
}

A Hidden Visual Studio Trick: Execute Selected Code with double Ctrl+E
What do to with Zone.Identifier files after importing a WSL machine and copying data
Support for SharePoint Server 2019 ends on Tuesday, July 14, 2026