{"id":12111,"date":"2011-11-12T00:01:00","date_gmt":"2011-11-12T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/11\/12\/use-powershell-to-edit-sharepoint-profiles\/"},"modified":"2011-11-12T00:01:00","modified_gmt":"2011-11-12T00:01:00","slug":"use-powershell-to-edit-sharepoint-profiles","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-edit-sharepoint-profiles\/","title":{"rendered":"Use PowerShell to Edit SharePoint Profiles"},"content":{"rendered":"<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\"><strong>Summary:<\/strong> Microsoft Premier Field Engineer Chris Weaver shows how to use Windows PowerShell to edit SharePoint profiles.<\/span><\/span><\/p>\n<\/p>\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">Microsoft Scripting Guy Ed Wilson here. It is Guest Blogger Weekend. To start the weekend, we have Chris Weaver. The <\/span><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/scripting%20wife\/\"><span style=\"font-family: Segoe\">Scripting Wife<\/span><\/a><span style=\"font-family: Segoe\"> and I recently met Chris <\/span><a href=\"http:\/\/en.wikipedia.org\/wiki\/Real_life\"><span style=\"font-family: Segoe\">in real life (IRL)<\/span><\/a><span style=\"font-family: Segoe\"> at Geek Ready, a conference at which I was speaking. What can I say? Chris is cool! Here is his biography.<\/span><\/span><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n<\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><\/span>&nbsp;<\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1452.WES-11-12-11-1.jpg\"><img decoding=\"async\" style=\"border: 0px\" title=\"Photo of Chris Weaver\" alt=\"Photo of Chris Weaver\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1452.WES-11-12-11-1.jpg\" width=\"150\" \/><\/a><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">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 <\/span><a href=\"http:\/\/blogs.technet.com\/b\/christwe\/\"><span style=\"font-family: Segoe\">my own blog<\/span><\/a><span style=\"font-family: Segoe\">, which will look at issues with SharePoint and its supporting infrastructure. <\/span><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">I enjoy camping with my family and kite surfing in my spare time (yeah, right, who has any of that?).<\/span><span style=\"font-size: small\"><b><span style=\"font-family: Segoe\">&nbsp;<\/span><\/b><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><b><span style=\"font-family: Segoe\">Note<\/span><\/b><span style=\"font-family: Segoe\">&nbsp; &nbsp;The script for today&rsquo;s blog article appears on the <\/span><a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/Setting-the-SPS-EmailOptin-53877534\"><span style=\"font-family: Segoe\">Scripting Guys Script Repository<\/span><\/a><span style=\"font-family: Segoe\">. <\/span><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">I am working with a customer to automate the installation and configuration of SharePoint Server 2010 using Windows PowerShell.<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">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.<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">We will be editing the profile property called <b>SPS-MailOptIn<\/b>. In the GUI, it is represented by three check boxes. <\/span><span style=\"font-family: Segoe;font-size: small\">First, you want to load the following assembly so that we can work with the profiles:<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">Add-Type -Path &#8220;c:\\program files\\common files\\microsoft shared\\web server extensions\\14\\isapi\\microsoft.office.server.dll&#8221;<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">And don&rsquo;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:<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">$PSSnapIn = Get-PSSnapin | where {$_.Name -like &#8220;*sharepoint*&#8221;}<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">if(!($PSSnapIn.Name -like &#8220;*sharepoint*&#8221;))<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">{<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">Add-PSSnapin Microsoft.SharePoint.PowerShell<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">}<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">Then we need to load the <b>UserProfileManager<\/b>, 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:<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">$Site = Get-SPSite -Limit 1&nbsp;&nbsp;&nbsp; #Get Site for Service context<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">$ServiceContext = Get-SPServiceContext($Site)<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">$ProfileManager = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($ServiceContext)<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">$Profiles = $ProfileManager.GetEnumerator()&nbsp;&nbsp;&nbsp; #Load all profiles into array<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">Using <b>Get-SPSite<\/b> 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 <b>-Limit<\/b> set to <b>1<\/b>, I get one site to connect to and don&rsquo;t have to worry about personalizing it to the farm before running the script.<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">Then I throw everything into a <b>For <\/b>loop, iterate through all the profiles, and get the property using the following:<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">$OriginalValue = $Profile[&#8220;SPS-EmailOptin&#8221;].Value<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">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:<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">$Profile[&#8220;SPS-EmailOptin&#8221;].Value = $EmailOptInValue<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">The variable <b>$EmailOptInValue<\/b> 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:<\/span><\/p>\n<ul>\n<li><span style=\"font-family: Segoe;font-size: small\">The first digit is for &ldquo;Notify me when someone leaves a note on my profile.&rdquo;<\/span><\/li>\n<li><span style=\"font-family: Segoe;font-size: small\">The second digit is for &ldquo;Notify me when someone adds me as a colleague.&rdquo;<\/span><\/li>\n<li><span style=\"font-family: Segoe;font-size: small\">The third digit is for &ldquo;Send me suggestions for new colleagues and keywords.&rdquo;<\/span><\/li>\n<\/ul>\n<p><span style=\"font-family: Segoe;font-size: small\">Finally, I commit the change, which I have wrapped in an <b>If<\/b> statement. This allows me to have <b>whatif<\/b> functionality, which was important for my customer:<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">if(!($Whatif))<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">{<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">$Profile.Commit()<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">}<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">I hope this function helps you to improve your SharePoint environment through the use of Windows PowerShell. <\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">Thanks, Chris, for writing this blog and sharing your script with us. <\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">I invite you to follow me on <\/span><a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\"><span style=\"font-family: Segoe\">Twitter<\/span><\/a><span style=\"font-family: Segoe\"> and <\/span><a href=\"http:\/\/bit.ly\/scriptingguysfacebook\"><span style=\"font-family: Segoe\">Facebook<\/span><\/a><span style=\"font-family: Segoe\">. If you have any questions, send email to me at <\/span><a href=\"mailto:scripter@microsoft.com\" target=\"_blank\"><span style=\"font-family: Segoe\">scripter@microsoft.com<\/span><\/a><span style=\"font-family: Segoe\">, or post your questions on the <\/span><a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\"><span style=\"font-family: Segoe\">Official Scripting Guys Forum<\/span><\/a><span style=\"font-family: Segoe\">. See you tomorrow. Until then, peace.<\/span><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n<p><span style=\"font-size: small\"><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><\/span>&nbsp;<\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[297,56,3,59,61,45],"class_list":["post-12111","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-chris-weaver","tag-guest-blogger","tag-scripting-guy","tag-sharepoint","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12111","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=12111"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12111\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=12111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=12111"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=12111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}