Find Orphan User E-mails in List Items using PowerShell

Our customized application for sending newsletters organizational wide, keeps its list of users to send e-mail in a SharePoint list called “Subscriptions”. Users are configured in a people picker field of the list. Now, when someone leaves the organization, their account become orphan and their E-mails also goes invalid.

So prior processing the E-mails column, we had to scan the people picker column in the list for orphaned user Emails.

PowerShell script to scan for orphaned Users from their Emails:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
Import-Module ActiveDirectory
$Web= Get-SPWeb ""
$List = $Web.Lists["Subscriptions"]

foreach($item in $List.Items)
  if($Item[$FieldName] -ne $null)
   #Get People picker field values collection
   $UserCollection = New-Object Microsoft.Sharepoint.SPFieldUserValueCollection($Web,$Item[$FieldName].ToString())
        #Get each User from the Person or Group field
        foreach($UserObj in $UserCollection)
            #Try to get the user from AD from Email 
            $ADUser= Get-ADUser -Filter {mail -eq $UserObj.User.Email}
            #check if user email doesn't exist in AD
            if($ADUser -eq $null)
                "$($Item['ID'])" + $UserObj.User.LoginName + $UserObj.User.Email

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

Leave a Reply