Navigate Up
Sign In
Supporters of Developer
Web

Using Powershell to...query the User Profile Service in SharePoint 2010

Item is currently unrated. Press SHIFT+ENTER to rate this item.1 star selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+ESCAPE to leave rating submit mode.2 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.3 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.4 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.5 stars selected. Press SHIFT+ENTER to submit. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.

You may also be interested in: O'Reilly - SharePoint 2010 at Work

 

Editor's note: Contributor Craig Pilkenton is a Senior Microsoft Consultant for CDW.com.

In this article we'll continue using the SharePoint management cmdlets, this time to interact with the 'dreaded' User Profile Service.

In my previous article, Using Powershell to... upload documents in SharePoint 2010, I talked about the basics of how to start using Powershell and some best practices.

In this one we'll jump directly to using the object model to talk to the User Profile Service (UPS).  You've probably heard the horror stories of UPS making grown men cry or run off to become hermits.  While it is an 'experience opportunity' working with the UPS, it is a very powerful storage engine of employee-to-employee discoverability.

Here's the file we'll be working from in this example:

2012-04-04-PowerShellToQuery-01.png

I've designed this script to output the results to the screen in semi-colon separated format so that I can copy it into Excel and give to the customer for review, but as always, you can do anything with the results that Powershell can handle.  I've marked up the file to denote what each section does but I'll call out the important areas below by line number.

  • Line 1: this is an example for calling the file with parameters and their format. The third parameter, $rqstrUserProps, is where you pass in a semi-colon separated string of which User Profile parameters to pull out.
    • If you don't have quick access to Central Admin of your SharePoint Farm, just uncomment Line 28 and all the Out-of-Box properties will show along with any custom ones as well.
  • Lines 2-6: receive passed-in parameters make this  script reusable so it can be called by other master scripts and/or be called as a Scheduled Task--
    • SharePoint MySite URL
    • Logon Domain of the users you wish to pull-in.  This is only important if you have users in the UPS store that logon with different domains
    • Semi-colon separated list of UPS properties to output
  • Line 12: takes the input parameter of properties and split's it into an enumerable array of values
  • Lines 18-19: this section uses the new Get-SPSite cmdlet
    • Store a reference to the MySite in the $site variable
    • Store a reference to Site Context using the new Get-SPServiceContext cmdlet in the $context variable
  • Lines 20-21: this section obtains a reference to the UserProfileManager using the SharePoint Object Model (no special cmdlet here), then split's all the profiles into an enumerable array
  • Line 23:  here we start looping through all the individual profiles to get the target properties
  • Lines 24-25: (optional) only needed as mentioned above
  • Lines 30-32: while we are inside our Profiles loop, we fire another looping using the array of properties from Line 12 above to query that users profile for the property’s value and push it into a string variable created earlier
  • Line 37: finally after all the looping is done we output the content to the screen to copy-paste into Excel
    • This section could also output the content into a .csv file, send an email with it as an attachment, inject into a database, or any other need we have.

With the pieces documented out, let's open a Powershell command to navigate to the directory where your scripts will live (e.g. "CD E:\Powershell\" + Enter, etc.), enter the name of the script, and the parameters to start running the script:


.\Get_UserProfile_Properties.ps1 "http://MySite_Url" "mydomain\" "LogonID;DisplayName;Title;Department;Office;Manager"

2012-04-04-PowerShellToQuery-02.png

So that's it.  Powershell takes our parameters and outputs the data we told it to, giving us user data that can be fed to other user-based systems as needed.  As usual, Powershell makes a tough task easy and repeatable.

A copy of the Powershell code, along with many others, is available at my SkyDrive location and the file is called Get_UserProfile_Properties.ps1.

Categories: PowerShell; User Profile Service; MOSS; WSS; 2010

Comments

Rhys

Another Tip

A tip that I found doing the same thing is that you don't actually have to pass in the my site host URL.  You can just use the central admin web application.  I like doing it this way as you don't need to edit values for different environments. Snippet below.
 
# Get Central Admin details
$CAWebApp = Get-spwebapplication -includecentraladministration | where {$_.IsAdministrationWebApplication}
$SiteUrl = $CAWebApp.url
#Connect to the User Profile Manager
$site = Get-SPSite $SiteUrl
$context = Get-SPServiceContext $site
$profileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($context)
$profiles = $profileManager.GetEnumerator()

Posted 10-Apr-2012 by Rhys
lionel

Nice but

Nice script man ! thanks for Sharing.
I think it can be a good idea to output only the data and not the comment about the current process. In this case we can directly pipe your script result to a csv file for further analysing ...

Posted 11-Apr-2012 by lionel
Kyle

Using Data

Your script worked really well, but I'm having two issues. One is that the end results are far to long for my powershell buffer, and the second is that I'd like to know if I can export the data to a csv from within the script. Thanks!

Posted 19-Sep-2012 by Kyle
Jan

Very nice

Working like a charm. Thanks for sharing.

Posted 03-Oct-2012 by Jan

Notify me of comments to this article

E-mail:
   

Add Comment

Title:

 
Comment:
Email:

   


Name:

 
Url: