I want to delete all lists with title this and this using PowerShell but getting error
Add-PSSnapin Microsoft.SharePoint.Powershell
Get-SPWebApplication https://portal.fruits.com | Get-SPSite -Limit All | Get-SPWeb -Limit All | where {$_.Url -Match "crates"} | ForEach-Object {
foreach ($list in $_.Lists) {
if ($list.Title -eq "FruitDocuments")
{
Write-Host($_.Url + "----" + $list.Title + "------" + $list.BaseTemplate)
$list.AllowDeletion = $true
$list.Update()
$list.Delete()
Write-Host("Deleted")
}
elseif ($list.Title -eq "Fruits")
{
Write-Host($_.Url + "----" + $list.Title + "------" + $list.BaseTemplate)
$list.AllowDeletion = $true
$list.Update()
$list.Delete()
Write-Host("Deleted")
}
}
}
Error
https://portal.fruits.com/crates/5678----Fruits------10005
Deleted
Collection was modified; enumeration operation may not execute.
At line:5 char:15
+ foreach ($list in $_.Lists) {
+ ~~~~~
+ CategoryInfo : OperationStopped: (:) [], InvalidOperationException
+ FullyQualifiedErrorId : System.InvalidOperationException