Saturday, January 14, 2012

Powershell script to print User Profile properties in a .csv file


Here is the powershell script to print all User Profile properties for a SharePoint farm and print them into a .csv file


1. Copy the code below and modify the variables highlighted in yellow below, save the following as PrintAllUserProfileProperties.ps1 file:

param
(
    $siteAddress
)

Add-PsSnapin "Microsoft.SharePoint.PowerShell"
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.UserProfiles")

try
{
    $site = New-Object Microsoft.SharePoint.SPSite $siteAddress
    $context = [Microsoft.SharePoint.SPServiceContext]::GetContext($site)
    $configManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileConfigManager $context
    $propertyManager = $configManager.ProfilePropertyManager
    set-variable -option constant -name out -value "C:\PrintAllUserProfileProperties.csv"

    foreach ($codeProperty in $propertyManager.GetCoreProperties())
    {
        if (!$codeProperty.IsSection)
        {
            $codeProperty.DisplayName + " - " + $codeProperty.Name | Out-File $out -append
        }
    }
}
finally
{
    if ($site -ne $null)
    {
        $site.Dispose()
    }
}

Echo "Finished!"
Echo "User Profile properties printed at:" $out

Remove-PsSnapin "Microsoft.SharePoint.Powershell"

2. To automatically run the above .ps1 script as a batch utility, Copy and paste the code below and save it with a .bat file extension, kindly make sure you update the siteAddress switch with the relevant farm URL

cd /d %~dp0
powershell -noexit -file ".\PrintAllUserProfileProperties.ps1" -siteAddress "http://dev-sp-2010:1000/sites/SPFix" "%CD%"
pause

Run the above .bat file, on the receipt of success message, traverse to the configured path and find the .csv file with all the user profile properties printed.