>Remove inactive users from site collection

>

In my company we started a domain migration. All users from one subdomain are moved to another domain. A perfect job for our sysadmins! What’s my role in this? Well, after moving the users they forgot to run the STSADM.exe –migrateuser tool so all the users are duplicated in our site collection. With the following code I cleaned the mess:

            Dim siteUrl As String = ConfigurationManager.AppSettings("siteUrl")

 

            Dim accountsToDelete As New List(Of SPUser)

 

 

            Using site As New SPSite(siteUrl)

 

                ‘show all VES users

                Using theWeb As SPWeb = site.OpenWeb()

 

                    For Each user As SPUser In theWeb.SiteUsers()

                        If user.LoginName.StartsWith("VES") Then

                            accountsToDelete.Add(user)

                        End If

                    Next

 

 

                    Console.WriteLine("Number of users found: {0}", accountsToDelete.Count)

                    Console.WriteLine("Delete all? [ y / n ]")

 

                    If Console.ReadKey.KeyChar = "y" Then

                        Console.WriteLine(Environment.NewLine)

                        Console.WriteLine("Deleting {0} users", accountsToDelete.Count)

                        ‘delete users

                        For Each u As SPUser In accountsToDelete

                            Console.Write("REMOVING :{0}…", u.LoginName)

                            theWeb.SiteUsers.Remove(u.LoginName)

                            theWeb.Update()

                            Console.WriteLine("Ok!")

                        Next

                    End If

 

 

 

 

                    theWeb.Close()

                    theWeb.Dispose()

                End Using

 

                site.Close()

                site.Dispose()

 

            End Using