The script it grew out of, and why orphaned SIDs pile up in the first place, is in Understanding Orphaned SIDs in Active Directory.