Sharepoint 2010 Delete Multiple Site Collection and Databases using Powershell

Some times you need to delete multiple site collections or databases so if we delete them manually its a very time taking process so here is a power shell script that will help you definitely.

clear
Remove-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
Add-PSSnapin Microsoft.SharePoint.Powershell

## Loading variables
$WebApplicationURL = "Web Application Url"
$tmpRoot = Get-SPWebApplication -Identity $WebApplicationURL
$DataBaseInstanceToDelete ="Database1,Database2,Database3,Database4,Database5"   ## Please Modify the array to delete Database instances
$SiteCollectionsToDelete = "Site Collection Url1, Site Collection Url2, Site Collection Url3, Site Collection Url4, Site Collection Url5"    ## Please Modify the array to delete Site Collections

## Getting all the databases related to web application

$DataBaseInstance = Get-SPContentDatabase -WebApplication $WebApplicationURL

## Enumerate through all the database instances and deleting them
foreach($item in $DataBaseInstance)
{
    if($DataBaseInstanceToDelete.Contains($item.Name))
   {
       $database = Get-SPContentDatabase $item.Id
       Write-Output "Deleting ContentDataBase:"+ $item.Name
       Remove-SPContentDatabase $database -Confirm: $fasle
    }
}

## Deleting Site Collection

$tmpRootColl=$tmpRoot.Sites

#Enumerate through each site collection

for ($index=$tmpRootColl.Count-1 ; $index-ge 0 ; $index–-)
{
      if($SiteCollectionsToDelete.Contains($($tmpRootColl.Item($index).Url)))
      {            
           Write-Output "Deleting Site Collection: $($tmpRootColl.Item($index).Url)"
           Remove-SPSite -Identity $tmpRootColl.Item($index) -GradualDelete -Confirm:$false       
      }    
}

Comments

Popular posts from this blog

C# Copy files from one server to another

Export SharePoint Online Group Users to Excel using Powershell