The script below works for a small SharePoint list but I cannot seem to get it to work with a large list over 900,000 rows.
Any suggestions would be greatly appreciated. I am very new to PowerShell but I think I am missing the loop.
$SiteName = "$list = "Audit Only Test"
$items = Get-PnPListItem -List $listName -Fields "Title","Created","ID","ShiftDate"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin
#Define Query to get list items
$Query= "<View Scope='RecursiveAll'>
<Query>
<Where>
<And>
<Lt>
<FieldRef Name='ShiftDate' Type='DateTime'/>
<Value Type='DateTime' IncludeTimeValue='TRUE'>
<Today OffsetDays='-14'/>
</Value>
</Lt>
<Eq>
<FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value>
</Eq>
</And>
</Where>
</Query>
<RowLimit>500</RowLimit>
</View>"
#Get All Items from the List in batches
$ListItems = Get-PnPListItem -List $ListName -Query $Query -PageSize 500
#Write-host "Total Number of Items Found:"$ListItems.count
foreach($item in $listItems)
{
Write-Host "Deleting Item - $($item.Id)"
#$item.deleteobject()
Remove-PnPListItem -List $listName -Identity $item.Id -Force -ErrorAction Stop
}
Write-Host -f Green "`nAll List Items Deleted Successfully!"
Any suggestions would be greatly appreciated. I am very new to PowerShell but I think I am missing the loop.
$SiteName = "$list = "Audit Only Test"
$items = Get-PnPListItem -List $listName -Fields "Title","Created","ID","ShiftDate"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin
#Define Query to get list items
$Query= "<View Scope='RecursiveAll'>
<Query>
<Where>
<And>
<Lt>
<FieldRef Name='ShiftDate' Type='DateTime'/>
<Value Type='DateTime' IncludeTimeValue='TRUE'>
<Today OffsetDays='-14'/>
</Value>
</Lt>
<Eq>
<FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value>
</Eq>
</And>
</Where>
</Query>
<RowLimit>500</RowLimit>
</View>"
#Get All Items from the List in batches
$ListItems = Get-PnPListItem -List $ListName -Query $Query -PageSize 500
#Write-host "Total Number of Items Found:"$ListItems.count
foreach($item in $listItems)
{
Write-Host "Deleting Item - $($item.Id)"
#$item.deleteobject()
Remove-PnPListItem -List $listName -Identity $item.Id -Force -ErrorAction Stop
}
Write-Host -f Green "`nAll List Items Deleted Successfully!"