SharePoint 2010: Viewing, Sorting, and Filtering SharePoint User Profiles Using PowerShell

SharePoint 2010: Viewing, Sorting, and Filtering SharePoint User Profiles Using PowerShell



Introduction


A question was asked in the MSDN forums about listing out all of the user profiles that don't have the PictureURL property set. The answer supplied was a slightly modified version of a script from the blog of Matthew Yarlett,  which lists out all of the user profiles without the PictureURL. This article takes that script and extends it further to provide richer examples of filtering and manipulating user profile data.

The Basics: Getting all the User Profiles


To get all the user profiles, create a new instance of the UserProfileManager object, passing it the SPServiceContext of the Central Admin site. The UserProfileManager contains a method, GetEnumerator, which returns an IEnumerator that can be used to iterate through all profiles.

[void][reflection.assembly]::Loadwithpartialname("Microsoft.Office.Server") | out-null;                        
$site=new-object Microsoft.SharePoint.SPSite("https://c05470sp10:7443"); #Central Admin site                        
$serviceContext = Get-SPServiceContext $site                     
$site.Dispose();                        
$upm = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($serviceContext);                        
$pc = $upm.GetEnumerator();

Using the collection of user profiles, we can then view, sort and filter user profiles rather easily. The following are some examples:

Example: List all the User Profiles


$pc = $upm.GetEnumerator();            
$pc | FT DisplayName

Example: List all the User Profiles, Including the Account Name


The account name is one the user profiles properties, in the Properties collection. User profile properties can be accessed directly from the UserProfile object by name, as userprofileobject["propertyname"]. We can leverage this in Format-Table by using a Hash Table (that contains an expression) to get the profile property we want.

First, by examining the UserProfile object, we can see it has a collection of properties:



We can use these properties in Format-Table by putting them in a Hash Table (which contains a Label for the value and an Expression that gets the value). There's more information about using Hash Tables with PowerShell on the TechNet site, and here's an example: Displaying Process Information in a Custom Table

The hash table can be created like this:

$hashTableForAccountName = @{Label="AccountName"; Expression={$_["AccountName"]}};

In this example, we pass the Hash Table to FT (Format-Table) inline.

$pc = $upm.GetEnumerator();            
$pc | FT DisplayName,@{Label="AccountName"; Expression={$_["AccountName"]}};

Example: List all the User Profiles, including the Users Department and Job Title

List all the user profiles including the users department and job title.

$pc = $upm.GetEnumerator();            
$pc | FT DisplayName,@{Label="Department"; Expression={$_["Department"]}},@{Label="Job Title"; Expression={$_["SPS-JobTitle"]}};

List all the user profiles including the users department and job title, sorting the values by department (using
Sort-Object). 

$pc = $upm.GetEnumerator();            
$pc | Sort -property  @{Expression={$_["Department"]}} | FT DisplayName,@{Label="Department"; Expression={$_["Department"]}},@{Label="Job Title"; Expression={$_["SPS-JobTitle"]}};




List all the user profiles including the account name and department, sort the values by department (using Sort-Object), and filtering the user profiles to users in the IT department (using the Where-Object commandlet alias command "?"). 

$pc = $upm.GetEnumerator();            
$pc | Sort -property  @{Expression={$_["Department"]}} | ?{$_["Department"] -like "I.T."} | FT DisplayName,@{Label="Department"; Expression={$_["Department"]}},@{Label="Job Title"; Expression={$_["SPS-JobTitle"]}};





Example: List all User Profiles with a Blank PictureURL Property


List all the user profiles that don't have a PictureURL set.

$pc = $upm.GetEnumerator();            
$pc | Sort -property  @{Expression={$_["FirstName"]}} | ?{$_["PictureUrl"].Value -eq $null -or $_["PictureUrl"].Value -eq ""} | FT DisplayName;

References


Other Languages


This article is also available in the following languages:

See Also


An important place to find a huge amount of SharePoint related articles is the TechNet Wiki itself. The best entry point is SharePoint Resources on the TechNet Wiki
Sort by: Published Date | Most Recent | Most Useful
Comments
Page 1 of 2 (14 items) 12