Delete Sub sites Using PowerShell SharePoint 2013

Delete Sub sites Using PowerShell SharePoint 2013

Recently I had to delete 50plus sub sites from one of my site collection which was created through script specified here.

All my sites had a naming conversion with a number which helped me to select the site dynamically.

Add-PSSnapin Microsoft.SharePoint.PowerShell

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null


$sequencestart =3000;
$maxNumber = 3100;
$deleteCount =0;

$siteURLPrefix = "http://get-spnote.com/subsite-";
for($i=$sequencestart ; $i -lt $maxNumber; $i++)
{
    $url = $siteURLPrefix+$i;
    Write-Host $url;

    #Gets the Sub site in to vairable, If Site is not existing Error Action parameter will not throw an exception
    $web = Get-SPWeb -Identity $url -ErrorAction SilentlyContinue;

        IF($web)
        {
            Write-Host "Site Exists..."
            Write-Host $url "is getting deleted" -f Yellow;
            Remove-SPWeb -Identity $url -Confirm:$false;
            Write-Host $url "dleted successfully.." -f Green;
            $deleteCount ++;

        }
        else
        {
            Write-Host $url " Site Not Exists" -f Red ;
        }     

       $web ="";

}
Write-Host $deleteCount " Sites Deleted";

Hope This might help someone to save

Leave a Reply

Your email address will not be published. Required fields are marked *