title: PowerShell - Enumerating through a collection and change the collection
body: <p>How it is posible to fix this script?</p>
  
  <p>Yes, I´m changing the collection in the foreach loop and this is the reason for this error.</p>
  
  <p><strong>An error occurred while enumerating through a collection: Collection was modified; enumeration operation may not execute..
  At C:\Users\user\Documents\PowerShell\ChangeAllListsV2.ps1:47 char:20
  +             foreach &lt;&lt;&lt;&lt; ($list in $webLists)
      + CategoryInfo          : InvalidOperation: (Microsoft.Share...on+SPEnumerator:SPEnumerator) [], RuntimeException
      + FullyQualifiedErrorId : BadEnumeration</strong></p>
  
  <pre><code>#Script change in all lists the required field property "testfield" to false
  
  
  #Part 0 - Configuration
  
  $urlWebApp = "http://dev.sharepoint.com"
  $countFound = 0
  $countList = 0
  $countFoundAndChange = 0
  
  #Part 1 - PreScript  
  
  $snapin = Get-PSSnapin | Where-Object {$_.Name -eq "Microsoft.SharePoint.Powershell"}
  
  if ($snapin -eq $null)
  
  {
      Write-Host “Loading SharePoint Powershell”
      Add-PSSnapin Microsoft.SharePoint.Powershell
  }
  
  #Part 2 - Script
  
  $webApp = Get-SPWebApplication $urlWebApp
  
  #$webApp | fl
  
      $webAppSites = $webApp.sites
  
      foreach($site in $webAppSites)
      {
          Write-Host "***********************************************************************"
          Write-Host "Found site: " $site -foreground blue
  
          $siteAllWebs = $site.AllWebs
  
          foreach($web in $siteAllWebs)
          {
              Write-Host "Found web: " $web -foreground blue
              #$web | fl
  
             $webLists = $web.Lists
  
              foreach($list in $webLists)
              {
               $countList ++
  
               Write-Host "Found list: " $list -foreground blue
  
                  #Change list property
  
                  $field = $Null
                  $field = $list.Fields["testfield"]
  
                      if($field){
                      Write-Host "Field found: " $list -foreground green
                      #Write-Host "in web: " $web -foreground green
                      $countFound ++
  
                          try{
  
                              if($field.Required)
                              {
  
                              #######################################################
                              $field.Required = $False
                              $field.Update()
                              #######################################################
  
                              $field = $Null
                              Write-Host "Done!: Change list: " $list -foreground  green
                              $countFoundAndChange ++                    
  
                              }else{ 
                              Write-Host "Already!: Change list: " $list -foreground  green       
  
                              }
  
                          }
                          catch{
                              $field = $Null
                              Write-Host "Error!: Change list: " $list -foreground red
                              Write-Host "in web: " $web -foreground red
                              $_
  
                          }
  
                      }
  
  
              } 
  
  
          }
  
  
      }
  
  
  
  Write-Host "Found lists: " $countList
  Write-Host "Found lists with column [testfield]: " $countFound
  Write-Host "Change lists with column [testfield]: " $countFoundAndChange
  </code></pre>