Thursday 4 July 2013

Viewing, sorting and filtering SharePoint User Profiles using PowerShell

I wrote a post a while back that had a snippet of PowerShell code that enumerated all the SharePoint User Profiles, looking for badly formatted PictureURL properties (here: http://matthewyarlett.blogspot.co.uk/2012/09/profile-photos-not-synchronising-from.html).

Today someone asked a question in the MSDN forums about listing out all the user profiles that don't have the PictureURL property set. I supplied a slightly modified version of this script to list out all the user profiles without the PictureURL as an answer. It made me think of other uses for this, and I thought I'd share them here.

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. We can display this information using FT (Format-Table) by specifying the field as an expression.

UserProfile object (note the Properties Collection property)



Using the Property in an expression. User profile property can be accessed directly from the UserProfile object by name, as userprofileobject["propertyname"]. We can leverage this in Format-Table by using an expression.
$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.

$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, and filtering the user profiles to users in the IT department.

$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; 





No comments:

Post a Comment