• I would make two changes.

    One, I would make is from using master..sysdatabases to sys.databases.

    Second, I would use the sys.server_principal and sys.database_principal tables to identify the orphaned logins. I wrote something just like that but the code is on secure networks back in Afghanistan so I can't readily show it without rewriting it. Maybe later.

    Other than that, not a bad script.