Wednesday, December 30, 2015

Find Orphan User E-mails in List Items using PowerShell

Requirement: 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 "https://portal.crescent.com/News/"
$List = $Web.Lists["Subscriptions"]
$FieldName="Members"

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)
             {
                "https://portal.crescent.com/News/Pages/ViewUser.aspx?UserId=$($Item['ID'])" + $UserObj.User.LoginName + $UserObj.User.Email
             }
        }
    }
}



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...