Change SharePoint My Sites URL From On-premises to Online

When the User Profile Service Application is created during SharePoint installation with default settings, the My Sites URL changes to online and that is cumbersome, so delete the default created User Profile service application and create a new one.

When creating the User profile service application, provide your site Host URL to the online URL host that would automatically change the My Sites URL at the Active Directory.

If the User Profile service application was created outside of the installation, then go to the User Profile Service Application, then My Sites settings and Setup My Sites. Then change the My Sites Host Location.

Once updated, the My Sites Host URL is updated in the Active Directory and is not given at SharePoint Central Administration.

Copy the following code and save it with the file name SetMySiteHostURLinAD.ps1.

  1. function PrintUsage {@”  
  2.     NAME: SetMySiteHostURLInAD.ps1  
  3.     SYNOPSIS: The purpose of this script is to set My Site Host URL in Active Directory.  
  4.     This URL will be returned through Exchange Autodiscover.  
  5.     MySiteHostURL–URL of My Site Host to set in Active Directory.  
  6.     Or use - get to get My Site Host URL from Active Directory.  
  7.     Or use - remove to remove My Site Host URL from Active Directory.  
  8.     SYNTAX: SetMySiteHostURLInAD.ps1“MySiteHostURL” | -get | -remove  
  9.     EXAMPLES: SetMySiteHostURLInAD.ps1“http: //my”  
  10.     SetMySiteHostURLInAD.ps1 - get  
  11.     SetMySiteHostURLInAD.ps1 - remove“@  
  12. }  
  13.   
  14. function GetConfigurationNamingContextPath {  
  15.     return GetEntryProperty“LDAP: //RootDSE” “configurationNamingContext”  
  16. }  
  17.   
  18. function GetExchangePath {  
  19.     param([string] $configurationNamingContextPath)  
  20.     return“LDAP: //CN=Microsoft Exchange,CN=Services,” + $configurationNamingContextPath  
  21. }  
  22.   
  23. function GetOrganizationContainerPath {  
  24.     param([string] $exchangePath)[string] $organizationContainerPath = “” ([ADSI] $exchangePath).Children | foreach {  
  25.         if (!$organizationContainerPath - and $_.SchemaClassName - eq“msExchOrganizationContainer”) {  
  26.             $organizationContainerPath = $_.Path  
  27.         }  
  28.     }  
  29.     return $organizationContainerPath  
  30. }  
  31.   
  32. function GetEntryProperty {  
  33.     param([string] $entryPath, [string] $propertyName)  
  34.     $entry = [ADSI] $entryPath[string] $value = “”  
  35.     trap {  
  36.         continue  
  37.     }  
  38.     $value = $entry.Get($propertyName)  
  39.     return $value  
  40. }  
  41.   
  42. function SetEntryProperty {  
  43.     param([string] $entryPath, [string] $propertyName, [string] $propertyValue)  
  44.     $entry = [ADSI] $entryPath  
  45.     if (!$propertyValue) {  
  46.         $entry.PutEx(1, $propertyName, $null)  
  47.     } else {  
  48.         $entry.Put($propertyName, $propertyValue)  
  49.     }  
  50.     trap {  
  51.         Write - Host“`nError setting property” - ForegroundColor Red  
  52.         continue  
  53.     }  
  54.     $entry.SetInfo()  
  55. }  
  56.   
  57. function AddOrReplaceOrRemoveMySiteHostURL {  
  58.     param([string] $old, [string] $url)[string] $separator = “;” [string] $label = “SPMySiteHostURL” + $separator  
  59.     if (!$old) {  
  60.         if (!$url) {  
  61.             return“”  
  62.         } else {  
  63.             return $label + $url  
  64.         }  
  65.     }  
  66.     [int] $labelPosition = $old.IndexOf($label)  
  67.     if ($labelPosition - eq - 1) {  
  68.         if (!$url) {  
  69.             return $old  
  70.         } else {  
  71.             if ($old[$old.Length–1] - eq $separator) {  
  72.                 return $old + $label + $url  
  73.             } else {  
  74.                 return $old + $separator + $label + $url  
  75.             }  
  76.         }  
  77.     }  
  78.     [int] $valuePosition = $labelPosition + $label.Length[int] $nextLabelPosition = $old.IndexOf($separator, $valuePosition)  
  79.     if ($nextLabelPosition - eq - 1) {  
  80.         if (!$url) {  
  81.             if ($labelPosition - eq 0) {  
  82.                 return“”  
  83.             } else {  
  84.                 return $old.Substring(0, $labelPosition–1)  
  85.             }  
  86.         } else {  
  87.             return $old.Substring(0, $valuePosition) + $url  
  88.         }  
  89.     }  
  90.     if (!$url) {  
  91.         return $old.Substring(0, $labelPosition) + $old.Substring($nextLabelPosition + 1)  
  92.     } else {  
  93.         return $old.Substring(0, $valuePosition) + $url + $old.Substring($nextLabelPosition)  
  94.     }  
  95. }  
  96. if ($args.Count - ne 1) {  
  97.     Write - Host“`nError: Required argument missing or too many arguments” - ForegroundColor Red  
  98.     PrintUsage  
  99.     exit  
  100. }  
  101. if ($args[0] - eq“ - ? ” - or $args[0] - eq“ - h” - or $args[0] - eq“ - help”) {  
  102.     PrintUsage  
  103.     exit  
  104. }  
  105. [string] $url = “”  
  106. if ($args[0] - ne“ - r” - and $args[0] - ne“ - remove”) {  
  107.     $url = $args[0]  
  108. }  
  109. Write - Host“`nSetting My Site Host URL in Active Directory…” [string] $configurationNamingContextPath = GetConfigurationNamingContextPath  
  110. Write - Host“`nConfiguration Naming Context path: $configurationNamingContextPath” [string] $exchangePath = GetExchangePath $configurationNamingContextPath  
  111. Write - Host“`nExchange path: $exchangePath” [string] $organizationContainerPath = GetOrganizationContainerPath $exchangePath  
  112. Write - Host“`nOrganization Container path: $organizationContainerPath” [string] $propertyName = “msExchServiceEndPointURL”  
  113. Write - Host“`nProperty name: $propertyName” [string] $old = GetEntryProperty $organizationContainerPath $propertyName  
  114. Write - Host“`nOld value: $old”  
  115. if (!$url) {  
  116.     Write - Host“`nRemoving value”  
  117. }  
  118. elseif($url - eq“ - g” - or $url - eq“ - get”) {  
  119.     Write - Host“”  
  120.     exit  
  121. else {  
  122.     Write - Host“`nAdding or replacing value: $url”  
  123. }  
  124. [string] $new = AddOrReplaceOrRemoveMySiteHostURL $old $url  
  125. Write - Host“`nNew value: $new”  
  126. SetEntryProperty $organizationContainerPath $propertyName $new  
  127. Write - Host“”  
Note: This script would only work in Exchange Server 2013 and will not work with Exchange Server 2010 because the script tries to update the property “msExchServiceEndPointURL” that is new in Exchange Server 2013.
  1. Go to Exchange Server and open Exchange Management Shell.
  2. Navigate the URL to the location where the script file was saved.
  3. Run the script with new mysite URL.

For example if the file is saved in the C drive and the new site URL is: http://NewMySiteURLOnline/sites/my, then
[PS] C:/> C:/SetMySiteHostURLInAD.ps1 http://NewMySiteURLOnline/sites/my

The preceding execution will update the My Sites Host URL at AD.

To verify, run the following script to check:

[PS] C:/> C:/SetMySiteHostURLInAD.ps -get.