Delete Last x days Sharepoint List item 


Add-PSSnapin Microsoft.SharePoint.PowerShell

[System.reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$web = Get-SPWeb ""
$list = $web.Lists["EmailNotification"]
$DeleteBeforeDate = [Microsoft.SharePoint.Utilities.SPUtility]::CreateISO8601DateTimeFromSystemDateTime([DateTime]::Now.AddDays(-7))
$caml=' {0} 1 ' -f $DeleteBeforeDate
$query=new-object Microsoft.SharePoint.SPQuery
$query.Query=$caml
$col=$list.GetItems($query)
$col | % {
$list.GetItemById($_.Id).Delete()
Write-Host "Deleted ID " $_.Id
}
Write-Host " Total Item  Deleted" +$col.Count

$web.Dispose()

Comments