Due to some requirement we need to disable the My Site Cleanup Timer Job , then we need to do manually clean up for those user is leaving organization.
Below is the script we can remove the user from user Profile (Profiles Missing from Import).
The reasons we remove it because we don't want other user click on the user leaving organization rout to user profile , want it point to user table information at site collection database.
More detail here :
https://blogs.msdn.microsoft.com/kaevans/2012/06/25/inside-the-sharepoint-2010-my-site-cleanup-timer-job/
========================================================================
if ((Get-PSSnapin “Microsoft.SharePoint.PowerShell” -ErrorAction SilentlyContinue) -eq $null) {
Add-PSSnapin “Microsoft.SharePoint.PowerShell”
}
$siteUrl = "xxxx"
$site = Get-SPSite $siteUrl
$context = Get-SPServiceContext($site)
$site.Dispose();
$pm = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($context);
$ProfileDB = Get-SPDatabase | ? { $_.Type -eq “Microsoft.Office.Server.Administration.ProfileDatabase”}
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = $ProfileDB.DatabaseConnectionString
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = 'select NTName,RecordId from UserProfile_Full where bDeleted=1'
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$SqlConnection.Close();
Write-host 'TotalCount: ', $DataSet.Tables[0].Rows.Count
Write-Host 'Following Inactive Accounts will be deleted !'
foreach($user in $DataSet.Tables[0].Rows)
{
write-host 'Planning to delete :' $user['NTName'], ',', $user['RecordId'] -ForegroundColor Green
$profile = $pm.GetProfile($user['RecordId'])
write-host $profile.DisplayName;
#To enable delete operation remove comment out for below line
#$pm.RemoveProfile($profile)
write-host $user['NTName'] is deleted!!! -ForegroundColor Red
}
write-host 'Operation Completed !'
========================================================================