Get User Account By Display Name in SharePoint
Requirement: Get user by display name in SharePoint
We have a CSV file with the list of projects, and their Team leads information. This list is to be updated on a SharePoint list. But the challenge is ‘Team Lead’ field has display names of the users instead of the account name (Domain\LoginID). So, before updating to the SharePoint list, we need the Login ID of the user from his display name.
Solution: Let’s Query Active directory for the given display name to get the user’s Login ID.
Prerequisites: You need to have the PowerShell module for the 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 the display name and update SharePoint List:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
Import-Module ActiveDirectory
#Configuration Variables
$SiteURL = "https://intranet.crescent.com/"
$ListName = "Projects"
$FieldName="Team Lead"
$CSVFile ="C:\Temp\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()
}
else
{
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)
{
$TeamLead=$web.ensureuser($UserAccount)
#Update Team member field
$item["Team Lead"] = $TeamLead
$item.Update()
Write-Host "Updated Project:"$Row.ProjectName -ForegroundColor Green
}
else
{
write-host "No matching User Account Found for :"$Row.TeamLead -f Red
}
}
else
{
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 "https://intranet.crescent.com"
$DisplayName="Barunx Romeih"
$Principal = [Microsoft.SharePoint.Utilities.SPUtility]::ResolvePrincipal($web, $DisplayName, "All", "All", $null, $false)
$Principal.LoginName
the above script is not working to get identity parameter in production site but works in dev environment any suggestion
You are so awesome! I needed the ResolvePrincipal in my script to keep from manually updating a field.
Thank you!