PowerShell to clean up user information in SharePoint 2013/2016

This PowerShell SharePoint tutorial, we will discuss how to clean up user information in SharePoint 2013/2016 site collection.

As part of my SharePoint FARM cleanup, I had noticed that there are many user account hanging in SharePoint who are not part of organization anymore. The business user had an escalation about populating nonexisting users while sharing sites / adding new users to the site.

To be more precise I have the user name “Raghava P” as part of members group but the same user is not part of AD users (as shown in below screenshots).

PowerShell to clean up user information in SharePoint

User existence in the members group

powershell cleanup user information sharepoint 2016

This is only for one site collection and one user I can’ ask the admin to navigate each and every site collection and delete the non-existed users.

Where does this user exist?

The strange doubt/question in this scenario from where this user is getting populated? In SharePoint as soon as you add a user with some permission the user will be added to a hidden list http://sharepoint13:12345/_catalogs/users/simple.aspx. That is why Microsoft is suggesting to add users from AD groups.

PowerShell to clean up user information in SharePoint 2013/2016

I have implemented the below PowerShell solution to clean the non-existed users across the SharePoint site collections.

The beauty of this script is! It read through a CSV file and delete all the users listed in that CSV file across all the site collections of a FARM.

([Parameter(Mandatory=$true)][ValidateScript({Test-Path $_ -Include "*.csv"})][String]$CSVPath)
#This script will remove users specified in the CSV.
$CSVFile = Import-CSV $CSVPath
Add-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue
#Get all site collections
$objSites = Get-SPSite -Limit All
$objAllSites = @()
foreach($objUser in $CSVFile)
{
    foreach($objSites in $objSites)
    {
        #Get the rootweb for the site collection
        $RootWeb = $objSites.RootWeb
        If([bool](Get-SPUser $objUser.Username -Web $RootWeb -EA SilentlyContinue) -eq $True)
        {
            #Remove the user from the User Information List
            Remove-SPUser -Identity $objUser.username -Web $RootWeb -Confirm:$False
            $objAllSites += $RootWeb.Url
        }
    }
    if(!($objAllSites).count -eq 0)
    {
        #Give feedback on deleted users
        Write-Host "Removed user $($objUser.username) from:" -Fore "Magenta"
        foreach($S in $objAllSites){Write-Host "- $S"}
        Write-Host ""
        $objAllSites = @()
    }
}

The output will be:

sharepoint 2016 orphaned users
sharepoint 2013 orphaned users

Input file format:

powershell cleanup user information sharepoint 2013

You may like following SharePoint PowerShell tutorials:

This PowerShell tutorial helps us to clean up user information in SharePoint 2013/2016.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

Krishna Vandanapu

I am Krishna.Vandanapu a SharePoint architect working in IT from last 13+ years, I worked in SharePoint 2007, 2010, 2013, 2016 and Office 365. I have extensive hands on experience in customizing SharePoint sites from end to end. Expertise in SharePoint migration tools like Sharegate, Doc Ave and Metalogix. Migrated SharePoint sites from SharePoint 2007 to 2010 and 2010 to 2013 several times seamlessly. Implementing CSOM with Microsoft best practices. Spent quality time in configuring SharePoint application services like User Profile, Search, Managed Meta data services etc. Now exploring SharePoint Framework and SharePoint 2019

>