How to delete SharePoint List with range month/year using PowerShell

Environment: SharePoint Server 2013 Tools: Power-shell Case: I’ve data in SharePoint List more than 5.000 items, it’s been started ...

Environment:
SharePoint Server 2013

Tools:
Power-shell

Case:
I’ve data in SharePoint List more than 5.000 items, it’s been started or created from 2017 until this post up. User want to delete the items in range January 2017 - December 2018. Please note, you can’t do that thing by manually with Select All > Delete, that’s wasted your time. Check below solutions!

How to get Field Name (Column Name) in SharePoint List/Library here

ps - afahru.com


Solutions:
Execute below script.
Add-PSSnapin Microsoft.SharePoint.PowerShell

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

$web = Get-SPWeb "http://contoso.com/"

$list = $web.Lists["Contoso List"]

$caml = '<Where><And><Geq><FieldRef Name="Created" /><Value IncludeTimeValue="TRUE" Type="DateTime">2017-01-01T00:00:00Z</Value></Geq><Leq><FieldRef Name="Created" /><Value IncludeTimeValue="TRUE" Type="DateTime">2018-12-31T23:59:59Z</Value></Leq></And></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()

Thanks :)

You Might Also Like

0 comments