0

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
0

You should get rid of foreach loop. Instead directly get the lists from collection and then delete them.

$FruitDocuments = $_.Lists["FruitDocuments"]
 if ($FruitDocuments -ne $null)
 {

        Write-Host($_.Url + "----" +   $FruitDocuments.Title + "------" + $FruitDocuments.BaseTemplate)
        $FruitDocuments.AllowDeletion = $true
        $FruitDocuments.Update()
        $FruitDocuments.Delete()
        Write-Host("Deleted")
 }
 $Fruits = $_.Lists["Fruits"]
 if ($Fruits -ne $null)
 {

        Write-Host($_.Url + "----" +   $Fruits.Title + "------" + $Fruits.BaseTemplate)
        $Fruits.AllowDeletion = $true
        $Fruits.Update()
        $Fruits.Delete()
        Write-Host("Deleted")
 }

UPDATE

Above code may throw error if the list with the given title is not present in a site. The issue can be avoided by using this script:

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  {

     for ($index = 0; $index -lt $_.Lists.Count; $index++) {
        if ($_.Lists[$index].Title -eq "FruitDocuments") {
            Write-Host($_.Url + "----" +   $_.Lists[$index].Title + "------" + $_.Lists[$index].BaseTemplate)
            $_.Lists[$index].AllowDeletion = $true
            $_.Lists[$index].Update()           
            $_.Lists[$index].Delete()
            Write-Host("Deleted")
        }
        elseif ($_.Lists[$index].Title-eq "Fruits") {
            Write-Host($_.Url + "----" +   $_.Lists[$index].Title + "------" + $_.Lists[$index].BaseTemplate)
            $_.Lists[$index].AllowDeletion = $true
            $_.Lists[$index].Update()           
            $_.Lists[$index].Delete()
            Write-Host("Deleted")
        }
    }
}
  • if you create a copy of the list by using @ as in foreach ($list in @($_.Lists)) the foreach works fine. Though yeah, if it's just 2 lists, no need to iterate... – Choggo Feb 2 '15 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.