Skip to content

Instantly share code, notes, and snippets.

@pkskelly
Created January 29, 2015 16:06
Show Gist options
  • Save pkskelly/7e814b634f18b652f1e2 to your computer and use it in GitHub Desktop.
Save pkskelly/7e814b634f18b652f1e2 to your computer and use it in GitHub Desktop.
Removes Orphaned Sites from a Content Database
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction "SilentlyContinue"
cls
Set-Alias -Name stsadm -Value $env:CommonProgramFiles”\Microsoft Shared\Web Server Extensions\15\BIN\STSADM.EXE”
function RemoveOrphanedSites([string]$WebAppUrl, [switch]$ReportOnly)
{
$configDB = Get-SPDatabase | where {$_.typename -eq "Configuration Database"}
$contentDBs = Get-SPWebApplication -IncludeCentralAdministration | Get-SPContentDatabase
foreach($contentDB in $contentDBs)
{
if ($WebAppUrl -ne "" -and $contentDB.WebApplication.Url.StartsWith($WebAppUrl) -eq $false)
{
continue
}
$webApplicationUrl = $contentDB.WebApplication.Url
Write-Host "webApplicationUrl=" $webApplicationUrl
[xml]$allwebs = stsadm -o enumallwebs -databasename $contentDB.Name
#$allwebs.OuterXML
$OrphanedSites = $allwebs.Databases.Database.site | ?{$_.insitemap -match "false"}
Foreach($site in $OrphanedSites)
{
if ($ReportOnly)
{
Write-Host "orphaned site, id=" $site.Id ", Url=" $site.Url ", not in sitemap"
}
else
{
Write-Host "orphaned site, id=" $site.Id ", Url=" $site.Url ", is removed from " $contentDB.Name
$contentDB.ForceDeleteSite($site.Id, $false, $false)
}
}
}
}
Start-SPAssignment -Global
#RemoveOrphanedSites "http://spserver.local" –ReportOnly
RemoveOrphanedSites "https://win12r2.tinman.local"
Stop-SPAssignment -Global
Write-Host ""
Write-Host "Finished! Press enter key to exit." -ForegroundColor Green
#Read-Host
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment