Last active
October 26, 2017 22:12
-
-
Save joerodgers/59e1de634f9550a71ae75ddc858d2fc4 to your computer and use it in GitHub Desktop.
Delete all SPWeb objects that are not provisioned.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add-PSSnapin Microsoft.SharePoint.PowerShell | |
# get all webs that are not read locked, not provisioned and created more than 5 minutes ago (so we don't delete anything that might be in the middle of provisioning) | |
$unprovisionedWebInfo = Get-SPSite -Limit All | ? { -not $_.ReadLocked } | Get-SPWeb -Limit All | ? { -not $_.Provisioned -and $_.Created.ToLocalTime().AddMinutes(5) -lt (Get-Date) } | SELECT URL, Created, WebTemplate, Provisioned, IsRootWeb | |
# write the data to csv for record keeping | |
$unprovisionedWebInfo | Export-Csv -Path "E:\UnprovisionedWebsToDelete.csv" -NoTypeInformation | |
foreach( $webInfo in $unprovisionedWebInfo ) | |
{ | |
# make sure the web still exists, it could have been deleted if it was a sub to a rootweb that was deleted. | |
$webExists = Get-SPWeb -Identity $webInfo.Url -ErrorAction SilentlyContinue | |
if( $webExists ) | |
{ | |
if( $webInfo.IsRootWeb ) | |
{ | |
Write-Host "Deleting Site: $($webInfo.Url)" | |
# delete the entire site since the rootweb is not provisioned | |
Remove-SPSite -Identity $webInfo.Url -Confirm:$false | |
} | |
else | |
{ | |
Write-Host "Deleting Web: $($webInfo.Url)" | |
# delete the subsite | |
Remove-SPWeb -Identity $webInfo.Url -Confirm:$false | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment