Thursday, July 5, 2012

PowerShell: Enumerate and Export User Profiles and Properties in SharePoint 2010

Good stuff for this latest blog post from me.

I recently had a request to validate the value of a particular set of properties for all user profiles in SharePoint 2010. PowerShell to the rescue! Save the following script to a .ps1 file.

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.UserProfiles")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

# SharePoint site URL
$site = new-object Microsoft.SharePoint.SPSite("http://site"); 

$ServiceContext = [Microsoft.SharePoint.SPServiceContext]::GetContext($site);   

$ProfileManager = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($ServiceContext)

$AllProfiles = $ProfileManager.GetEnumerator()

write-output "Display Name; AccountName; Supervisor"

foreach($profile in $AllProfiles)
{
$DisplayName = $profile.DisplayName
$AccountName = $profile[[Microsoft.Office.Server.UserProfiles.PropertyConstants]::AccountName].Value
$Supervisor = $profile["Supervisor"].value
write-output "$($DisplayName); $($AccountName); $($Supervisor)"
}

write-output "Finished."

$site.Dispose()


In my example above, I am getting the value for three properties: Display Name, Account Name, and Supervisor.

For the list of PropertyConstants members, see the following link:

http://msdn.microsoft.com/en-us/library/microsoft.office.server.userprofiles.propertyconstants_members.aspx

Export the output of this script to a CSV file by appending > filename.csv to the command. Therefore, the entire PowerShell command would be:

.\filename.ps1 > filename.csv