Power Shell Script to find and delete orphaned users from site

In this Post we will learn to find or delete orphaned users from SharePoint Site using Power Shell Script.

 First lets know Who are orphaned users?

Orphaned users are those who are disabled/removed from Active directory.but still have permission in SharePoint site ,lists and items. SharePoint keeps “user info” table of the content database for metadata such as created/modified by field.

It’s a best practice to keep farm clean  and organized also this will solve the problem active directory users still appearing on the people picker 

If we want delete few users only then browse to following URL of your site delete user from there.

 

http://server:2000/_layouts/people.aspx?MembershipGroupId=0 
   

from this we can get master list of user from site collection.if you remove the user.user will be removed from complete site collection

 

We can also query the users alternatively from SQL server

 

Step 1 : Open SQL Server Management Studio

 SELECT * FROM [MOSS_Content_DatabaseName].[dbo].[UserInfo] WHERE tp_Login=’DOMAIN\UserID’        

 

Step 2: Take note of the tp_ID column value

Step 3: Go to http://<your sharepoint-site-collection/_layouts/userdisp.aspx?ID=tp_ID, where tp_ID is the number you found from the above select statement.

 

Step 4: This will take you to the user’s profile where you can click on the Delete User from Site Collection button.

However it is very difficult to remove user one by one from site collection. alternatively we can adopt powershell script to find delete the orphaned users from site.