Find Orphaned SharePoint Items using PowerShell script

The script below helps you find the orphaned items from SharePoint content databases

if ((Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{Add-PSSnapin Microsoft.SharePoint.Powershell}


$CntDBs = Get-SPContentDatabase

ForEach ($CntDB in $CntDBs)
{   

      Write-Host "Detecting Orphans for " $CntDB.Name    $CntDB.Repair($false)
}


The above script will only list down the Orphaned items. You  can also repair these by changing

$CntDB.Repair($false) to $CntDB.Repair($true)

However before doing any such repair, you should be taking a back up of your DB.

2 comments:

  1. Many many thanks for this script.

    Items includes only site collections and sub sites within the content DBs?

    ReplyDelete
  2. Great Post!
    Thanks a lot for sharing!
    I found this blog to be very useful!!
    PowerShell training in Bangalore

    ReplyDelete