1

I've been trying to delete more than 10,000 list items in SharePoint using PowerShell. So far, this is what I've had

[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c") 
[System.Reflection.Assembly]::Load("Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
[System.Reflection.Assembly]::Load("Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
[System.Reflection.Assembly]::Load("System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")

# "Enter the site URL here"
$SITEURL = "http://sharepoint/site"

$site = new-object Microsoft.SharePoint.SPSite ( $SITEURL )
$web = $site.OpenWeb()
"Web is : " + $web.Title

# Enter name of the List below
$oList = $web.Lists["Test List"];

"List is :" + $oList.Title + " with item count " + $oList.ItemCount

$collListItems = $oList.Items;
$count = $collListItems.Count - 1

for($intIndex = $count; $intIndex -gt -1; $intIndex--)
{
        "Deleting : " + $intIndex
        $collListItems.Delete($intIndex);
}

This deletes all items in the list, but I need to filter the items to be deleted based on date, like for instance (date today - 90 days) upon last modified date.

0

ModifiedDate should be a regular old DateTime object, so you should be able to use this kind of thing:

$count = $collListItems.Count;
$date = get-date;
$ninetyDays = $date.AddDays(-90);

for($intIndex = 0; $intIndex -lt $count; $intIndex++)
{
    $listItem = $collListItems[$intIndex];
    if($listItem["ModifiedDate"] -lt $ninetyDays)
    {
        Write-Host "Deleting " $listItem.Name;
        $collListItems.Delete($intIndex);
    }
}

$collListItems.Update();

I would, of course, strongly recommend you test code snippet on one item first. You may need to cast $listItem["ModifiedDate"] as a DateTime object in order for this to work.

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.