4

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?

1

1 Answer 1

5
$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
    #$eventReceiversToDelete[$i].Delete()
}

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

4
  • Great answer, was just about to start writing this PS myself so you've saved me time :-) Apr 9, 2012 at 15:27
  • Getting this error "Collection was modified; enumeration operation may not execute"
    – Adam
    Sep 27, 2016 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. Sep 28, 2016 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, 2016 at 17:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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