If you are using AD Import
Run The below script, It will export all the sharepoint users email and check user email is present in Active Directory or not if the user id is not present in AD it will delete the users froms. SharePoint user profile.
I was facing the same issue so i have written a powershell script to resolve it.
Make changes in the script according to your need.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Web Appplication URL
$siteUrl = "https://sharepoint.net"
$Sites =Get-SPSite -Identity "$siteUrl"
#Creating Connection With SharePoint
$Context = Get-SPServiceContext -Site $siteUrl
$userprofileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($Context);
$Userprofiles = $userprofileManager.GetEnumerator()
#crating Empty Arrays to store Users not in AD and All the Sharepoint user ID emails
$Allemails = @()
$usersnotinAD = @()
$Userdetails = @()
$AllSites = @()
#Exporting SharePoint Users email IDs
foreach ($Userprofile in $Userprofiles) {
$StoreUsers = [PSCustomObject]@{
Workemail = $userprofile["workemail"].value
Accountname = $userprofile["AccountName"].value
}
$Userdetails += $StoreUsers
$Allemails += $userprofile["workemail"].value
}
#exporting Username and email id to csv
$Userdetails | Export-csv E:\scripts\UserProfile\usernameandemailid.csv -NoTypeInformation
#Filtering out the EmailID's Ending with google.net
#$emails = $Allemails | Where {$_ -like "*@google.net"}
foreach ($email in $Allemails) {
#verifying User profile has email id or not
try{
$ExistingADUser = Get-ADUser -Filter "EmailAddress -eq '$email'"
#if user is not present in AD it will perform below operations
if($null -eq $ExistingADUser)
{
# $user = $ExistingADUser.Name
write-host "$email does not yet exist in active directory" -BackgroundColor Red
$usersnotinAD += $email
$inputCSV ="E:\scripts\UserProfile\usernameandemailid.csv"
#import the csv file to dbrows
$dbRows = IMPORT-CSV $inputCSV
foreach ($dbRow in $dbRows)
{
if($dbRow.Workemail -eq $email)
{
#Fetch user Profile
$UserProfile = $UserProfileManager.GetUserProfile($dbRow.Accountname)
#remove user profile
$UserProfileManager.RemoveUserProfile($dbRow.Accountname);
#Remove User from the UIL
foreach($Site in $Sites)
{
#Get the rootweb for the site collection
$RootWeb = $Site.RootWeb
$passname = $dbRow.Accountname
#change prefix as per user profile
$userpreffix = "i:0#.w|"
$newname = $userpreffix + $passname
#Remove the user from the User Information List
Remove-SPUser -Identity $newname -Web $RootWeb -Confirm:$False
}
}
}
}
else
{
write-host "$email exists in active directory" -BackgroundColor Green
}
}
catch [System.Exception]{
write-host "Email id is not present for the user " -BackgroundColor red
}
}
$usersnotinAD | Out-File E:\scripts\UserProfile\usersnotinAD.txt
$Allemails | Out-File E:\scripts\UserProfile\ExportedSharePointEmailIDs.txt