Get User Account By Display Name in SharePoint

Requirement: Get user by display name in SharePoint
We have a CSV file with list of projects and their Team leads information. This list to be updated on a SharePoint list. But the challenge is 'Team Lead' field has display names of users instead of account name (Domain\LoginID). So prior updating to SharePoint list, we need the Login ID of the user from his display name.

Solution: Lets Query Active directory for the given display name to get the user's Login ID.

Prerequisites: You need to have PowerShell module for Active directory installed to use: Import-Module ActiveDirectory! Use PowerShell cmdlet: Add-WindowsFeature RSAT-AD-PowerShell to add AD Module for PowerShell to your server/desktop! Otherwise, you'll get an error message: "Import-Module : The specified module 'ActiveDirectory' was not loaded because no valid module file was found in any module directory"

PowerShell script to get user accounts from display name and update SharePoint List:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
Import-Module ActiveDirectory

#Configuration Variables
$SiteURL = ""
$ListName = "Projects"                
$FieldName="Team Lead"
$CSVFile ="C:\TeamLeads.csv" 

#Custom Function Get User Account from Display Name in AD 
Function Get-UserAccount($DisplayName)
    $UserAccount=Get-ADUser -LDAPFilter "(displayName=$DisplayName)" | Select sAMAccountName
    if($UserAccount.sAMAccountName -ne $null)
        return $UserAccount.sAMAccountName.tostring()
        write-host $DisplayName not found in AD! -f Red
        return $null
#Import from CSV file - CSV has Headers ("ProjectName", "TeamLead")
$CSVData = Import-CSV -path $CSVFile

#Get the Target Web & List
$Web = Get-SPWeb -identity $WebURL
$List = $web.Lists[$ListName]
#Iterate through each Row in the CSV file
foreach ($Row in $CSVData) 
    #Filter by Project Name
    $Item = $List.Items | Where-Object { $_["Project Name"] -eq $Row.ProjectName }
    #If the matching project found
    If($Item -ne $null)
        write-host "Searching for:"$Row.TeamLead
        #Get the User Account from Display Name
        $UserAccount = Get-UserAccount $Row.TeamLead
        #If User account found in AD
        if($UserAccount -ne $null)
            #Update Team member field
            $item["Team Lead"] = $TeamLead
            Write-Host "Updated Project:"$Row.ProjectName -ForegroundColor Green
            write-host "No matching User Account Found for :"$Row.TeamLead -f Red
        write-host "No matching List Item Found for:"$Row.ProjectName -f Red

Using SPUtility's ResolvePrincipal Method:
Instead of querying Active directory, you can also use SPUtility's ResolvePrincipal method to get a user by display name:
$Web = Get-SPWeb ""
$DisplayName="Barunx Romeih"
$Principal = [Microsoft.SharePoint.Utilities.SPUtility]::ResolvePrincipal($web, $DisplayName, "All", "All", $null, $false)


  1. You are so awesome! I needed the ResolvePrincipal in my script to keep from manually updating a field.
    Thank you!

  2. the above script is not working to get identity parameter in production site but works in dev environment any suggestion


Please Login and comment to get your questions answered!

Powered by Blogger.