Use PowerShell to Edit SharePoint Profiles

Use PowerShell to Edit SharePoint Profiles

  • Comments 7
  • Likes

Summary: Microsoft Premier Field Engineer Chris Weaver shows how to use Windows PowerShell to edit SharePoint profiles.

Microsoft Scripting Guy Ed Wilson here. It is Guest Blogger Weekend. To start the weekend, we have Chris Weaver. The Scripting Wife and I recently met Chris in real life (IRL) at Geek Ready, a conference at which I was speaking. What can I say? Chris is cool! Here is his biography. 

 

Photo of Chris Weaver

I have been working at Microsoft since late 2008. During that time I have been an engineer within CSS, a SharePoint 2010 TAP, and most recently a dedicated premier field engineer working with several of our Premier customers to support their SharePoint infrastructure. I have been using Windows PowerShell for the last two years to simplify the administration and troubleshooting of SharePoint for my customers. I very recently started my own blog, which will look at issues with SharePoint and its supporting infrastructure.

I enjoy camping with my family and kite surfing in my spare time (yeah, right, who has any of that?). 

Note   The script for today’s blog article appears on the Scripting Guys Script Repository.

 

I am working with a customer to automate the installation and configuration of SharePoint Server 2010 using Windows PowerShell.

I would like to show you one of the functions that we are using to configure profiles after we have completed an import. The customer is highly regulated and has users that cannot communicate in any form with other users, so when it was discovered that profiles by default would send email for several different reasons, we had to reverse it.

We will be editing the profile property called SPS-MailOptIn. In the GUI, it is represented by three check boxes. First, you want to load the following assembly so that we can work with the profiles:

Add-Type -Path "c:\program files\common files\microsoft shared\web server extensions\14\isapi\microsoft.office.server.dll"

And don’t forget to ensure that the SharePoint snap-in is loaded because we will want to use commands from it. You can use the following lines to verify or load that info. This snap-in is installed on all SharePoint installations and automatically loaded, if using the SharePoint 2010 Management Shell:

$PSSnapIn = Get-PSSnapin | where {$_.Name -like "*sharepoint*"}

 

if(!($PSSnapIn.Name -like "*sharepoint*"))

{

Add-PSSnapin Microsoft.SharePoint.PowerShell

}

Then we need to load the UserProfileManager, and I do so with the following code. To be able to manage this, the account running the script will need to be a User Profile Service Application Administrator with at least Manage Profile permissions:

$Site = Get-SPSite -Limit 1    #Get Site for Service context

$ServiceContext = Get-SPServiceContext($Site)

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

$Profiles = $ProfileManager.GetEnumerator()    #Load all profiles into array

Using Get-SPSite has two advantages. The first advantage is that I do not have to worry about disposing of the object because Windows PowerShell will do that for me. The second advantage is that by using the switch -Limit set to 1, I get one site to connect to and don’t have to worry about personalizing it to the farm before running the script.

Then I throw everything into a For loop, iterate through all the profiles, and get the property using the following:

$OriginalValue = $Profile["SPS-EmailOptin"].Value

After I get the value, I save it to a text file to ensure that I have it in the future, if needed. Then, as I had mentioned, only some users are regulated, so we compare the profiles name to a list of regulated users, and if not in the list, we proceed to change the value with the following line:

$Profile["SPS-EmailOptin"].Value = $EmailOptInValue

The variable $EmailOptInValue represents three integers. Each number can be 0 or 1. 0 means the box is checked, and 1 means it is not. As an example, the variable could be loaded with the 101 or 010:

  • The first digit is for “Notify me when someone leaves a note on my profile.”
  • The second digit is for “Notify me when someone adds me as a colleague.”
  • The third digit is for “Send me suggestions for new colleagues and keywords.”

Finally, I commit the change, which I have wrapped in an If statement. This allows me to have whatif functionality, which was important for my customer:

if(!($Whatif))

{

$Profile.Commit()

}

I hope this function helps you to improve your SharePoint environment through the use of Windows PowerShell.

 

Thanks, Chris, for writing this blog and sharing your script with us.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

 

Ed Wilson, Microsoft Scripting Guy

 

 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • I've been trying to do this but I keep getting an error when creating the UserProfileManager object:

    New-Object : Exception calling ".ctor" with "1" argument(s): "Object reference not set to an instance of an object."

    At line:1 char:29

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

       + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodInvocationException

       + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

    My $ServiceContext object looks like this:

    SiteSubscriptionId : 00000000-0000-0000-0000-000000000000

    Any ideas?

  • Hello Adam,

    I would suggest that you look at the following items since I believe that your not getting the correct Service Context

    1. Do you have at least full read on the Site Collection that you are pointing to?

    2. Insure you are pointing at the Site Collection object (http://webapp/sitecollection/) and not a list, page or library.

    3. Insure that the Site Collection has the UPA included in it's proxy group

    4. You have to have the proper permissions on the UPA so either add your account or use an elevated account.

    Let me know if this helps or if you get other errors.

    Thank you,

    Chris Weaver

  • Hi,

    Thanks for nice article.

    For new USers how to disable this property? Do we need to run after adding new user or is there any work around?

    Regards

    Srikanth

  • Hi,

    Can you please explain how this property value can be interpretted in sharepoint 2013?

    In Sharepoint 2013 the value is stored as integer and if multiple checkboxes are checked then it is returning a single integer value.

    I couldn't find a documentation from MS on how to interpret this.

    Thanks,

    Soumyadev

  • Hey Chris,

    When an Admin gets "Object reference not set to an instance of an object."  it is because they can not retrieve the information, this is usually due to incorrect permissions on the UPA.

    Fix:

    ------

    Go to Central Administration

    Select Application Management

    Under Service Application select Manage Service Applications

    Highlight your User Profile

    Select Administrators and you are probably listed there (If not then add account)

    Select User Permissions and add yourself there as well. (You will only get Full permission option)

    Yes you actually need both or you will fail.

    Awesome article!

    Jeff

  • I am also wondering if this can be re-used for 2013?

  • Though there is six check box in a screen of SPS2013, actually, is Email_Notifications seven?

    If all boxes get a check; EmailOptInValue=1(0000001?)

    If all boxes do not get a check; EmailOptInValue=126(1111110?)

    When I carried it out in "111111", a check was not off only one(Someone replied to my community discussion post)