Top Menu

Tuesday, January 14, 2014

Export Sharepoint User Profiles to CSV

I was looking for PowerShell script to export SharePoint User Profile data to a CSV and found this beautiful script by John Lynch.

I have made following little modification to the script:
  1. Export users which have special characters in their name by applying the UTF8 encoding.
  2. Check for manager field and show their display name instead of network id.
# Export Sharepoint User Profiles to CSV file
# Created: John Lynch 2013
# Updated: Tahir Naveed 2014
# MIT License

$siteUrl = "http://MySite.com"
$outputFile = "G:\UPAExport\UPAexport_20140428.csv"

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

Function GetDisplayName($UserName)
{
    $serviceContext = Get-SPServiceContext -Site $siteUrl
    $upm = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($serviceContext);
    $userProfile = $upm.GetUserProfile($UserName);
    $FullName = $userProfile.DisplayName
    return $FullName
}

$serviceContext = Get-SPServiceContext -Site $siteUrl
$profileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($serviceContext);
$profiles = $profileManager.GetEnumerator()

$fields = @(
            "UserName"
            "FirstName",
            "LastName",
            "PreferredName",
            "WorkPhone",
            "MobilePhoneFromAD",
            "HomePhone",
            "CellPhone",
            "Fax",
            "localTelephone",
            "SPS-Birthday",
            "WorkEmail",
            "PersonalSpace",
            "PictureURL",
            "Office",
            "title",
            "department",
            "manager",
            "SPS-School",
            "PersonalInterests",
            "LocationExperience",
            "HomeTown",
            "Assistant"
)

$collection = @()

foreach ($profile in $profiles) 
{
   $user = "" | select $fields
   foreach ($field in $fields) 
   {    
        if($profile[$field].Property.IsMultivalued) 
        {
            $user.$field = $profile[$field] -join "|"
        } 
        else 
        {
            if ($field -eq "manager" -or $field -eq "Assistant")
            {
                $DomainName = $profile[$field].Value;
                if ($DomainName -ne $null)
                {
                    $DisplayName = GetDisplayName $DomainName
                    $user.$field = $DisplayName
                }
            }
            else
            {
                $user.$field = $profile[$field].Value
            }
        }
       
   }
   $collection += $user
   Write-Host $user.UserName
}

$collection | Export-Csv $outputFile -NoTypeInformation -Encoding UTF8
$collection |  Out-GridView

No comments:

Post a Comment

Official SharePoint Documentation

I have recently contributed to the official SharePoint documentation for developement. Check it out here: https://docs.microsoft.com/en-us...