1

How do I get manager's name instead of sAMAccount? I was following the powershell link (see below) to populate a list.

I tried this $user.Item("Manager").Value; or $user.Item("Manager").Name; but no success

https://www.sharepointusecases.com/2012/11/use-case-export-user-profiles-to-a-sharepoint-list-phonebook-style/

-1

There is a post on this site How to get Display Name from Manager(Person field) userprofile - SharePoint 2010 and it helped. Here is the full code that works for my need.

# https://www.sharepointusecases.com/2012/11/use-case-export-user-profiles-to-a-sharepoint-list-phonebook-style
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
    Add-PSSnapin Microsoft.SharePoint.PowerShell;
}
function Enumerate-SPUserProfiles($webUrl, $listName)
{
    $x= [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
    $x= [System.Reflection.Assembly]::LoadWithPartialName("microsoft.sharepoint.portal")
    $x= [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server")
    $x= [System.Reflection.Assembly]::LoadWithPartialName("System.Web")
    $sites = Get-SPSite
    $context = [Microsoft.Office.Server.ServerContext]::GetContext($sites[0])
    $profileManager = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($context)
    $web = Get-SPWeb $webUrl
    $list = $web.Lists[$listName]
    $profileManager.GetEnumerator() | ?{$_.DisplayName -notlike '*\*' -and $_.ProfileType -eq "User" } | foreach-object { Update-PhoneBook $_ $web $list $webUrl }
}

function Update-PhoneBook($user, $web, $list, $webUrl)
{
    if($user.Item("FirstName") -ne "" -and $user.Item("FirstName") -ne $null -and $user.Item("EmployeeID") -ne $null)
    {
        $spQuery = new-object Microsoft.SharePoint.SPQuery
        $userName = $user.Item("UserName")
        $camlQuery = "<Where><Eq><FieldRef Name='UserName' /><Value Type='Text'>$userName</Value></Eq></Where>";
        $spQuery.Query = $camlQuery
        $spQuery.RowLimit = 1
        $spListItemCollection = $list.GetItems($spQuery)
        $domain = (gwmi WIN32_ComputerSystem).Domain

        if($spListItemCollection.Count -ge 1)
        {
            Write-Host "Updating $username..."
            Update-Contact $spListItemCollection[0] $user $domain $webUrl 
        }
        else
        {
            Write-Host "Adding $username..."
            $contact = $list.items.add();
            Update-Contact $contact $user $domain $webUrl 
        }
    }
}

function Update-Contact($contact, $user, $domain, $webUrl)
{
    $contact["Title"] = $user.Item("title");
    $contact["EmployeeName"] = $user.Item("PreferredName");
    $contact["Employee ID"] = $user.Item("EmployeeID");
    $contact["EmployeeLocation"] = $user.Item("Office");

    $sites2 = Get-SPSite $webUrl
    $context2 = [Microsoft.Office.Server.ServerContext]::GetContext($sites2[0])
    $profileManager2 = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($context2)
    $manager = $user.Item("Manager")
    $upSupervisor = $profileManager2.GetUserProfile($manager.Split('\')[1]);
    $contact["ManagersName"] =  $upSupervisor["PreferredName"];

    $contact["Department"] = $user.Item("Department");
    $contact["UserName"] = $user.Item("AccountName");

    $contact.update()
}

#You can run it by typing:
Enumerate-SPUserProfiles "https://train.thoshiba.br.pt" "Employees List TEST"
1
  • he already mentioned the same script / link in his question/ – Waqas Sarwar MVP Mar 6 '19 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.