Deleting Orphaned Event Receivers using PowerShell

While looking through the event logs at a client of mine the other day I came across an odd error that was occurring regularly. Apparently they had developed a Feature which contained an Event Receiver which was programmatically bound to a series of lists. As time went on their requirements changed so they deleted the event receiver class. Problem was that the binding to the lists still exists so every time an item was updated an error would be dumped to the event log about a missing type in the specified assembly.

Fortunately it’s pretty easy to fix this using some simple PowerShell:

The only thing that threw me off when I was putting this little snippet together was the fact that I couldn’t work with the collections directly due to errors about the collection being modified during the enumeration (so a “for” loop rather than a “foreach” loop would have worked just as easily as doing filtering as I am above).

The code above is great for when you know the class name of a specific event receiver that needs to be pulled. If you don’t know the name then you’ll have to add in some reflection to look for the assembly and type – just be careful as the assembly may or may not be in the GAC.

I noticed an issue when using this technique against a list that had only 1 feature receiver attached.

The value for $evts.Count was set to $null, not 1, because the $evts.GetType().BaseType was being returned as a “Microsoft.SharePoint.Administration.SPAutoSerializingObject” and not as a “System.Array”.

All worked fine for lists that had more than 1 event receiver attached.