Pages

Search This Blog

Wednesday, April 1, 2015

Deleting number of sub sites recursively in SharePoint

Deleting number of sub sites recursively in SharePoint

(Enter the Text File Path that contains Name of Sites to be cleared)

function RemoveSPWebRecursively($web)
{
    $subwebs = $web.Webs
    foreach($subweb in $subwebs)
    {
        RemoveSPWebRecursively($subweb)
        $subweb.Dispose()
    }
    Remove-SPWeb $web -Confirm:$false
}


Write-Host
Write-Host

write-host "Enter the SharePoint Site URL : "
[string]$URL = Read-Host
[string]$WebURL =  $URL.Trim()


$objSite = Get-SPWeb $WebURL -ErrorAction SilentlyContinue


if($objSite -eq $null)
{
               Write-Host
               Write-Host "Site URL not correct or Site does not extsts. Re-run the script with correct URL." -foregroundcolor Red
               Read-Host
}
else
{
        Write-Host
        Write-Host
           write-host "Enter the Text File Path that contains Sites list to be deleted : "
        [string]$FilePathTmp = Read-Host
        [string]$FilePath =  $FilePathTmp.Trim().Replace('"','')
        $Projects = Get-Content -Path $FilePath
        if($Projects -eq $null)
        {
            Write-Host
               Write-Host "Sorry! Unable to get contents from file." -foregroundcolor Red
               Read-Host
        }
        else
        {
            Write-Host
            write-host  "["  $Projects.count  "] Project(s) found in file. " -foregroundcolor Green
            Write-Host
            $TotalInFile=$Projects.count
            $TotalInWeb =$objSite.Webs.Count
            [int]$TotalDeleted=0

            if( $Projects.count -gt 0)
            {
                foreach ($ProjectSite in $Projects)
                {
                    if($ProjectSite -ne $null)
                    {
                        [bool]$SiteFound =$false;
                       
                        $ProjectSite=$ProjectSite.Trim()

                        for ($i = 0; $i -lt $objSite.Webs.Count; $i++)
                        {
                            $subSite = $objSite.Webs[$i];
                            if($subSite -ne $null)
                            {
                              if($subSite.Title -eq $ProjectSite)
                              {
                                $SiteFound =$true;
                                write-host "FOUND Project Site : " $ProjectSite -foregroundcolor Yellow
                                if( $subSite.Webs.Count -gt 0)
                                {
                                    RemoveSPWebRecursively $subSite
                                }
                                else
                                {
                                    Remove-SPWeb $subSite -Confirm:$False
                                }
                                $TotalDeleted=$TotalDeleted + 1
                                write-host "Project Site : " $ProjectSite " deleted successfully" -foregroundcolor Green
                                break
                              }
                            }
                            else
                            {
                                write-host "Site was NULL at : " $i -foregroundcolor Red
                            }
                        }

                        if($SiteFound -eq $false)
                        {
                            write-host "NOT FOUND Project Site : " $ProjectSite -foregroundcolor Red
                        }
                    }
                }
                <#
                foreach($subSite in $objSite.Webs)
                      {
                    foreach ($ProjectSite in $Projects)
                    {
                        if($ProjectSite -ne $null)
                        {
                            $ProjectSite=$ProjectSite.Trim()
                            if($subSite.Title -eq $ProjectSite)
                            {
                                write-host "Found Project Site : " $ProjectSite -foregroundcolor Green
                            }
                        }
                    }
                             $subSite.Dispose()
                      }#>

                Write-Host
                Write-Host "Sites Deleted Sucessfully....." -foregroundcolor Yellow
                Write-Host
                Write-Host "Total Sites Found in File to be deleted :  " $TotalInFile -foregroundcolor Green
                Write-Host "Total Sites Found in Web Application :  " $TotalInWeb -foregroundcolor Green
                Write-Host "Total Sites Deleted :  " $TotalDeleted -foregroundcolor Green

                Write-Host
                Write-Host "Press Enter to exit." -foregroundcolor Yellow
                   Read-Host
            }
            else
            {
                Write-Host
                Write-Host "No Sites list found in file.... Press Enter to exit." -foregroundcolor Red
                   Read-Host
            }
           
        }
        $objSite.Dispose()
}





No comments:

Post a Comment