Online Training On SharePoint
                      

Tuesday, 1 June 2010

Deleting Multiple Document Sets with PowerShell Script in SharePoint 2010

We have a test document library containing around 30000 Document Sets. We wanted to delete all the docsets but it is very time consuming to delete from UI or using the Object Model. This can be achieved using the following Power Shell Script. If you want to use this Script just change the first three parameters in the script:
### SET THESE ACCORDING TO YOUR NEEDS ###
$webUrl = "http://testsite"
$libraryName = "DemoDocSets"
$queryLimit = 100
$docsetContentTypeName = "Document Set"
#########################################
Write-Host -f Yellow "Opening Site and Document Library"
$web = Get-SPWeb $webUrl
$documentsLib = $web.Lists[$libraryName]
Write-Host -f Yellow "Done"
### Load SharePoint assemblies
Write-Host -f Yellow  "Loading SharePoint assemblies"
Add-PSSnapIn Microsoft.SharePoint.PowerShell
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

# get 1000 document sets at a time
$query = New-Object Microsoft.SharePoint.SPQuery
$query.RowLimit = $queryLimit
$query.ViewAttributes = "Scope=`"Recursive`"";
$query.Query = "<Where><Eq><FieldRef Name='ContentType'/><Value Type='Text'>" + $docsetContentTypeName + "</Value></Eq></Where>"

$queryResults = $documentsLib.GetItems($query)
$numDocsets = $queryResults.Count

while($numDocsets -gt 0)
{
    Write-Host -f Yellow "Deleting $numDocsets docsets"
    
    for($i=0; $i -lt $numDocsets; $i++)
    {
        $queryResults.Delete(0)
    }
    
    $queryResults = $documentsLib.GetItems($query)
    $numDocsets = $queryResults.Count
}
Write-Host -f Green "Done deleting all docsets"
Post a Comment
Related Posts with Thumbnails