Use PowerShell to Edit SharePoint Profiles

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