Lately i had a bug that time-to-time added duplicate event receiver entries in SPEventReceiverDefinitionCollection.

So how do i easily enumerate and remove these duplicate entries?

$eventReceiversToDelete = $list.EventReceivers | 
    group-object assembly, class, type | #Groups event receiver by assembly/class/type
    ? { $_.Count -gt 1 } |    #If any of assembly/class/type pairs are encountered more than once, we have a duplicate
    % { $_.Group[1..100] }  #enumerate starting from second item (indexed at 1), so we don't delete event receiver completely

for ($i = $eventReceiversToDelete.Count - 1; $i -ge 0; $i--) {
    Write-Host "Deleting $($eventReceiversToDelete[$i].Type) Event Receiver $($eventReceiversToDelete[$i].Name)" }
    #Uncomment to delete

Thanks to this blog post for showing the way: Finding (and Deleting) Duplicate Files

  • Great answer, was just about to start writing this PS myself so you've saved me time :-) – Brian Scott Apr 9 '12 at 15:27
  • Getting this error "Collection was modified; enumeration operation may not execute" – Adam Sep 27 '16 at 17:22
  • 1
    @Adam I tried to fix the script. Try to delete now. The problem is Foreach cannot be used for deletion - you must loop backwards within the collection to delete the entries. I haven't tested that script and hopefully no syntax errors there. – Janis Veinbergs Sep 28 '16 at 8:36
  • I ended up using a similar flavor to the change you made. It should work provided the syntax is correct. Thanks @JanisVeinbergs – Adam Sep 28 '16 at 17:06

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.