How to delete a SharePoint list's items using power-shell
cls
$Server ="Server URL"
$web = Get-SPWeb -Identity $Server;
Write-Host "Web is : " $web.Title -foregroundcolor yellow
$oList = $web.Lists.TryGetList("<LIST NAME>");
Write-Host "List is : " $oList.Title " with item count " $oList.ItemCount -foregroundcolor yellow
$spqQuery = New-Object Microsoft.SharePoint.SPQuery
$spqQuery.Query = "<Where><And><Geq><FieldRef Name='Date' /><Value Type='DateTime'>2015-07-01T00:00:00Z</Value></Geq>
<Leq><FieldRef Name='Date' /><Value Type='DateTime'>2015-07-31T00:00:00Z</Value></Leq>
</And></Where>"
#$spqQuery.ViewAttributes = "Scope='Recursive'"
$items = $oList.GetItems($spqQuery)
$collListItems = $oList.GetItems($spqQuery)
$count = $collListItems.Count - 1
Write-Host $collListItems.Count
$counter = 1;
$currentItem = 1;
$itemCount = $collListItems.Count;
for($intIndex = $count; $intIndex -gt -1; $intIndex--)
{
Write-Progress -Id 1 -ParentId 0 -Activity " Deleting item.. " -PercentComplete (($currentItem/$itemCount)*100) -Status " Deleting item $currentItem of $itemCount";
$currentItem++;
$collListItems.Delete($intIndex);
Write-Host ([String]::Format("Deleted record:{0}",$counter)) -foregroundcolor green;
$counter++;
}
Comments
Post a Comment