PowerShell – Querying User Profile Service Application – SharePoint Server 2013

This blog is quick way to show you how you can query your user profile service application for specific user. It is always easy to find user by using the UserName but in case where you do not have username available but the display name available e.g. someone giving you excel file to load data in SharePoint list.

In that case you can use the following script to query the user profile service application using the display name.

Here is the script.

 

Add-PSSnapin microsoft.sharepoint.powershell

$site = Get-SPSite "http://intranet.contoso.com/"

$web = $site.OpenWeb();

$context = Get-SPServiceContext $site;

$profileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($context);   

$searchPattern = “USER DISPLAY NAME”;   

$searchResults = $profileManager.Search($searchPattern);

foreach($result in $searchResults)
{
    write-host $result.AccountName;
}