4

Is there a quick powershell script to delete items from a list where created date is less than some date? Also, would the deleted items be in the recycle bin (in the event business team wants to restore some items).

  • Nice job finding the solution! Can you post it as an answer now? – Kit Menke Jul 29 '11 at 20:37
5

On behalf of author, the answer was found here: http://blogs.flexnetconsult.co.uk/colinbyrne/2008/02/26/PowerShellSharePointThreeWaysToDeleteAListItemIDCAMLStringMatching.aspx

[void][System.reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

$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() 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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