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

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

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 basic code for getting all the user profiles is:

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


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

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


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"]}};


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"]}};




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;

The question in the forums can be seen here:
There are some other Wiki articles that use PowerShell to manipulate the user profiles; Updating User Profile Properties with PowerShell in SharePoint 2010 and SharePoint 2010: Delete Manager from UserProfile

Leave a Comment
  • Please add 7 and 4 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Matthew Yarlett edited Original. Comment: Updated links, added links to other wiki pages and added some images.

Page 1 of 1 (1 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
Page 1 of 1 (13 items)