Powershell cleaning orphan objects in content DB

The below script help to identify and clean orphan objects in content databases.

clear
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)
}

this script will only show the orphan objects, you need to change the $CDB.Repart to true –   $CDB.Repair($false)  in order to repair. !!!   Backup your database before using this script !!!