PowerShell to refine user information in SharePoint 2013 and SharePoint 2016

PowerShell to refine user information in SharePoint 2013 and SharePoint 2016
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 any more. Business user had an escalation about populating non existing users while sharing sites / adding new users to site.
To be more precise I have user name “Raghava P” as part of members group but the same user is not part of AD users (as shown in below screen shots).
PowerShell to refine user information in SharePoint 2013 and SharePoint 2016
PowerShell to refine user information in SharePoint 2013 and SharePoint 2016

User existence in members group:

user information list powershell sharepoint 2013
user information list powershell sharepoint 2013
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 is this user existed??
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 the users from AD groups.
Solution:
I have implemented the below PowerShell solution to clean the non-existed users across the 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:
user information list powershell sharepoint 2016
user information list powershell sharepoint 2016
 SharePoint 2013 user information list powershell
SharePoint 2013 user information list powershell

Input file format:

SharePoint 2016 user information list powershell
SharePoint 2016 user information list powershell

Hope this helps !!! enjoy development in SharePoint.


You May Also like the Following SharePoint Online Tutorials:

About Krishna Vandanapu

I am Krishna.Vandanapu a SharePoint architect working in IT from last 12 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

View all posts by Krishna Vandanapu →