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. function GetConfigurationNamingContextPath {
  14. return GetEntryProperty“LDAP: //RootDSE” “configurationNamingContext”
  15. }
  16. function GetExchangePath {
  17. param([string] $configurationNamingContextPath)
  18. return“LDAP: //CN=Microsoft Exchange,CN=Services,” + $configurationNamingContextPath
  19. }
  20. function GetOrganizationContainerPath {
  21. param([string] $exchangePath)[string] $organizationContainerPath = “” ([ADSI] $exchangePath).Children | foreach {
  22. if (!$organizationContainerPath - and $_.SchemaClassName - eq“msExchOrganizationContainer”) {
  23. $organizationContainerPath = $_.Path
  24. }
  25. }
  26. return $organizationContainerPath
  27. }
  28. function GetEntryProperty {
  29. param([string] $entryPath, [string] $propertyName)
  30. $entry = [ADSI] $entryPath[string] $value = “”
  31. trap {
  32. continue
  33. }
  34. $value = $entry.Get($propertyName)
  35. return $value
  36. }
  37. function SetEntryProperty {
  38. param([string] $entryPath, [string] $propertyName, [string] $propertyValue)
  39. $entry = [ADSI] $entryPath
  40. if (!$propertyValue) {
  41. $entry.PutEx(1, $propertyName, $null)
  42. } else {
  43. $entry.Put($propertyName, $propertyValue)
  44. }
  45. trap {
  46. Write - Host“`nError setting property” - ForegroundColor Red
  47. continue
  48. }
  49. $entry.SetInfo()
  50. }
  51. function AddOrReplaceOrRemoveMySiteHostURL {
  52. param([string] $old, [string] $url)[string] $separator = “;” [string] $label = “SPMySiteHostURL” + $separator
  53. if (!$old) {
  54. if (!$url) {
  55. return“”
  56. } else {
  57. return $label + $url
  58. }
  59. }
  60. [int] $labelPosition = $old.IndexOf($label)
  61. if ($labelPosition - eq - 1) {
  62. if (!$url) {
  63. return $old
  64. } else {
  65. if ($old[$old.Length–1] - eq $separator) {
  66. return $old + $label + $url
  67. } else {
  68. return $old + $separator + $label + $url
  69. }
  70. }
  71. }
  72. [int] $valuePosition = $labelPosition + $label.Length[int] $nextLabelPosition = $old.IndexOf($separator, $valuePosition)
  73. if ($nextLabelPosition - eq - 1) {
  74. if (!$url) {
  75. if ($labelPosition - eq 0) {
  76. return“”
  77. } else {
  78. return $old.Substring(0, $labelPosition–1)
  79. }
  80. } else {
  81. return $old.Substring(0, $valuePosition) + $url
  82. }
  83. }
  84. if (!$url) {
  85. return $old.Substring(0, $labelPosition) + $old.Substring($nextLabelPosition + 1)
  86. } else {
  87. return $old.Substring(0, $valuePosition) + $url + $old.Substring($nextLabelPosition)
  88. }
  89. }
  90. if ($args.Count - ne 1) {
  91. Write - Host“`nError: Required argument missing or too many arguments” - ForegroundColor Red
  92. PrintUsage
  93. exit
  94. }
  95. if ($args[0] - eq“ - ? ” - or $args[0] - eq“ - h” - or $args[0] - eq“ - help”) {
  96. PrintUsage
  97. exit
  98. }
  99. [string] $url = “”
  100. if ($args[0] - ne“ - r” - and $args[0] - ne“ - remove”) {
  101. $url = $args[0]
  102. }
  103. Write - Host“`nSetting My Site Host URL in Active Directory…” [string] $configurationNamingContextPath = GetConfigurationNamingContextPath
  104. Write - Host“`nConfiguration Naming Context path: $configurationNamingContextPath” [string] $exchangePath = GetExchangePath $configurationNamingContextPath
  105. Write - Host“`nExchange path: $exchangePath” [string] $organizationContainerPath = GetOrganizationContainerPath $exchangePath
  106. Write - Host“`nOrganization Container path: $organizationContainerPath” [string] $propertyName = “msExchServiceEndPointURL”
  107. Write - Host“`nProperty name: $propertyName” [string] $old = GetEntryProperty $organizationContainerPath $propertyName
  108. Write - Host“`nOld value: $old”
  109. if (!$url) {
  110. Write - Host“`nRemoving value”
  111. }
  112. elseif($url - eq“ - g” - or $url - eq“ - get”) {
  113. Write - Host“”
  114. exit
  115. } else {
  116. Write - Host“`nAdding or replacing value: $url”
  117. }
  118. [string] $new = AddOrReplaceOrRemoveMySiteHostURL $old $url
  119. Write - Host“`nNew value: $new
  120. SetEntryProperty $organizationContainerPath $propertyName $new
  121. 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.