magritton
8/4/2014 - 2:51 PM

Removed or deletes a SharePoint site with PowerShell

Removed or deletes a SharePoint site with PowerShell

Add-PsSnapin Microsoft.SharePoint.PowerShell
# Completely deletes the specified Web (including all subsites)
function RemoveSPWebRecursively(
    [Microsoft.SharePoint.SPWeb] $web)
{
    Write-Debug "Removing site ($($web.Url))..."
    
    $subwebs = $web.GetSubwebsForCurrentUser()
    
    foreach($subweb in $subwebs)
    {
        RemoveSPWebRecursively($subweb)
        $subweb.Dispose()
    }
    
    $DebugPreference = "SilentlyContinue"
    Remove-SPWeb $web -Confirm:$false
    $DebugPreference = "Continue"
}

$DebugPreference = "SilentlyContinue"
$web = Get-SPWeb "http://portal.opwftg.com/sites/OPWSS/Teams/Safety/OES2"
$DebugPreference = "Continue"

If ($web -ne $null)
{
    RemoveSPWebRecursively $web
    $web.Dispose()
}
Remove-SPWeb "http://portal.opwftg.com/sites/OPWSS/Teams/Safety/FCC/Cells/Supermarket" -Confirm:$false