1

Is there a way to delete items from a document library based on some criteria for e.g. delete all documents whose created date is less than 1/1/2012.

I know it can be done through code but I am looking for some other way like powershell command just like we write an SQL statement such as:

DELETE FROM mylibrary WHERE created <= '1/1/2012'
2

1 Answer 1

0

mmm try this script:

$site = new-object Microsoft.SharePoint.SPSite("http://sitecollectionURL") 
$web = $site.rootweb 
$list = $web.Lists["Name of the List"]

$caml='<Where>
        <Leq>
            <FieldRef Name="Created" />
            <Value IncludeTimeValue="TRUE" Type="DateTime">2011-12-31T02:32:20Z</Value>
        </Leq>
    </Where>'

$query=new-object Microsoft.SharePoint.SPQuery 
$query.Query=$caml | Write-Output

$items=$list.GetItems($query) 
$listItemsTotal = $items.Count;

for($x=$listItemsTotal-1;$x -ge 0; $x--)
{
        $items[$x].Delete()
}

$web.Dispose() 
$site.Dispose() 

Good Luck

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.