Wednesday 7 August 2013

Delete SharePoint List Using Powershell

Delete SharePoint List using Power shell Script

Deleted SharePoint list names are fetching from Excel sheet.
 - In Excel shell there is one column name is "Name".

 Following is the power shell script code

if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
$count = 0
$contentWebAppServices = (Get-SPFarm).services |? {$_.typename -eq "Microsoft SharePoint Foundation Web Application"}
foreach($webApp in $contentWebAppServices.WebApplications)
{
    write-host "Site Collections"
    write-host ""
    foreach ($site in $webApp.Sites)
    {
       $count++
       write-host "Site Collections URL --> -->" $site.URL
       write-host ""
       write-host "SubSites"
       write-host ""
       if($site.AllWebs -ne 'null')
       {
     foreach ($web in $site.AllWebs)
       {
           $count++
       write-host "SubSite URL --> --> -->" $web.URL
       write-host ""
       $lists = $web.Lists
           for ($index = 0; $index -lt $lists.Count; $index++)
       {
               $SharePointListName= import-csv "C:\Users\test1\Desktop\PowerShell Script\SharePointListName.csv"
               $SharePointListName|ForEach-Object{
               $ListName=$_.'NAME'
               if ($lists[$index].Title -eq $ListName)
               {
                    $lists[$index].Delete()
                    Write-Host "Web = $web, List = $ListName "
                    $web.Update();
               }
           }
     }
       }
    }
}
}

write-host "Total Count :" $count


 

 




No comments:

Post a Comment