Tuesday, 26 March 2019

Delete SharePoint list items using powershell with caml query

Add-PSSnapin Microsoft.SharePoint.PowerShell

[System.reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$web = Get-SPWeb "SiteURL"
$list = $web.Lists["OTSubmitForm"]
$DeleteBeforeDate = [Microsoft.SharePoint.Utilities.SPUtility]::CreateISO8601DateTimeFromSystemDateTime([DateTime]::Now.AddDays(-37))
$caml='<Where> <Lt> <FieldRef Name="Created" /><Value Type="DateTime">2016-05-30T11:12:56Z</Value> </Lt> </Where>'
$query=new-object Microsoft.SharePoint.SPQuery
$query.Query=$caml
$col=$list.GetItems($query)
Write-Host $col.Count
$col | % {$list.GetItemById($_.Id).Delete()}
$web.Dispose()

No comments:

Post a Comment