Cleanup user information list and remove users from Site


Recently one of the analyst requested can we cleanup profiles in the User Information List in a site collection as he was looking to create a template out of it. This is an interesting topic because not only this is important for as a template but it is also an extranet site (internet facing) and its a good practice to minimize the user information exposed externally, most of the extranet site collections if not taken care the details can be queried by directly pointing the site collections to the following url’s

http://<site_collection_url>/_layouts/userdisp.aspx?Force=True&ID=20  (change the number to query groups and users and their personal details)

Coming back to the point how to delete user profiles in a site collection, and also cleanup the user information list, check the below powershell.

Note: It also deletes the users and user permission on the site collection.

$sc = Read-host "Enter Site Collection URL"
$site = get-spsite $sc
$users = get-spuser -web $site.rootweb -limit All
foreach ($user in $users){remove-spuser -identity $user.userlogin -web $site.rootweb -confirm:$false -erroraction Silentlycontinue }

Another approach to filter and cleanup the user information list below. This remove the groups as well if not filtered by the item.ID while passing the item to remove.

# Note: take a backup of your site collection before attempting this script - 
# Note: this script could strip all the users and their permissions on the site
$sc = Read-host "Enter Site Collection URL"
$web = get-spweb $sc
$list = $web.SiteUserInfoList
#this will get all the users and groups so be careful while passing $item to remove.
$items = $list.getitems()

foreach ($item in $items){$web.siteusers.RemoveByID($item.ID) -erroraction Silentlycontinue }

This is a simple post to achieve what I am looking and there is a potential to corrupt your site collections, I had taken care to test in my DEV farm. This has been tested in sharepoint 2010 sp1 only.

The below posts can help you further on this topic.

http://www.sharepointdiary.com/2012/04/delete-users-clean-up-user-information-list.html

http://blog.falchionconsulting.com/index.php/tag/set-spuser/