|
| 1 | +# Renaming a SharePoint Hub Site URL |
| 2 | + |
| 3 | +## Summary |
| 4 | + |
| 5 | +Renaming a SharePoint Hub Site URL or title is not a straightforward process. A hub site can not be renamed directly. Instead, the hub site needs to be unregistered before performing the rename, and then re-register it as a hub. This ensures the integrity of the hub structure and keeps associated sites intact. |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## 🛠️ What This Script Does |
| 10 | + |
| 11 | +This PowerShell script walks through: |
| 12 | + |
| 13 | +✅ Unregistering the hub site |
| 14 | + |
| 15 | +✏️ Renaming the site URL and title |
| 16 | + |
| 17 | +🗑️ Deleting the redirect site automatically created after renaming |
| 18 | + |
| 19 | +🔁 Re-registering the site as a hub |
| 20 | + |
| 21 | +🔍 Verifying that child sites remain associated |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +### Prerequisites |
| 26 | + |
| 27 | +- The user account that runs the script must have access to the SharePoint Online site. |
| 28 | + |
| 29 | +# [PnP PowerShell](#tab/pnpps) |
| 30 | + |
| 31 | +```powershell |
| 32 | +param ( |
| 33 | + [Parameter(Mandatory = $true)] |
| 34 | + [string]$domain = "contoso", |
| 35 | +
|
| 36 | + [Parameter(Mandatory = $true)] |
| 37 | + [string]$currentSiteUrl = "https://contoso.sharepoint.com/sites/TestHR_Hub", |
| 38 | +
|
| 39 | + [Parameter(Mandatory = $true)] |
| 40 | + [string]$updatedSiteUrl = "https://contoso.sharepoint.com/sites/TestHR_Hub_Updated" |
| 41 | +) |
| 42 | +
|
| 43 | +$adminSiteURL = "https://$domain-Admin.SharePoint.com" |
| 44 | +Connect-PnPOnline -Url $adminSiteURL |
| 45 | +
|
| 46 | +try { |
| 47 | + $siteId = (Get-PnPTenantSite -Identity $currentSiteUrl).SiteId |
| 48 | + $hubSite = Get-PnPHubSite -Identity $siteId.Guid -ErrorAction SilentlyContinue |
| 49 | +
|
| 50 | + if (-not $hubSite) { |
| 51 | + Write-Host "The site is not a hub site or does not exist." |
| 52 | + } |
| 53 | + else { |
| 54 | + $childSites = Get-PnPHubSiteChild -Identity $siteId.Guid -ErrorAction SilentlyContinue |
| 55 | + Unregister-PnPHubSite -Site $siteId.Guid |
| 56 | + } |
| 57 | +
|
| 58 | + Rename-PnPTenantSite -Identity $currentSiteUrl -NewSiteUrl $updatedSiteUrl -NewSiteTitle 'Test HR Hub' | Out-Null |
| 59 | +
|
| 60 | + # Wait for the rename to complete |
| 61 | + $updatehubSite = Get-PnPTenantSite -Identity $updatedSiteUrl -ErrorAction SilentlyContinue |
| 62 | + while(-not $updatehubSite) { |
| 63 | + Start-Sleep -Seconds 30 |
| 64 | + $updatehubSite = Get-PnPTenantSite -Identity $updatedSiteUrl -ErrorAction SilentlyContinue |
| 65 | + } |
| 66 | +
|
| 67 | + # Remove the redirect site created by the rename |
| 68 | + Remove-PnPTenantSite -Url $currentSiteUrl -Force |
| 69 | +
|
| 70 | + # Register the site as a hub site again |
| 71 | + Register-PnPHubSite -Site $updatedSiteUrl | Out-Null |
| 72 | +
|
| 73 | + # Check if child sites are still associated |
| 74 | + $childSites1 = Get-PnPHubSiteChild -Identity $siteId.Guid -ErrorAction SilentlyContinue |
| 75 | + $result = Compare-Object -ReferenceObject $childSites1 -DifferenceObject $childSites |
| 76 | + if ($result) { |
| 77 | + Write-Host "Child sites have been updated after renaming the hub site." |
| 78 | + } else { |
| 79 | + Write-Host "No changes in child sites after renaming the hub site." |
| 80 | + } |
| 81 | +} |
| 82 | +catch { |
| 83 | + Write-Host "An error occurred while renaming the hub site: $_" |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +[!INCLUDE [More about PnP PowerShell](../../docfx/includes/MORE-PNPPS.md)] |
| 88 | + |
| 89 | +*** |
| 90 | + |
| 91 | +## Source Credit |
| 92 | + |
| 93 | +Sample first appeared on [How to Safely Rename a SharePoint Hub Site URL with PnP PowerShell](https://reshmeeauckloo.com/posts/powershell-sharepoint-rename-hubsite/) |
| 94 | + |
| 95 | +## Contributors |
| 96 | + |
| 97 | +| Author(s) | |
| 98 | +|-----------| |
| 99 | +| [Reshmee Auckloo](https://github.com/reshmee011) | |
| 100 | + |
| 101 | + |
| 102 | +[!INCLUDE [DISCLAIMER](../../docfx/includes/DISCLAIMER.md)] |
| 103 | +<img src="https://m365-visitor-stats.azurewebsites.net/script-samples/scripts/spo-rename-hub-siteurl" aria-hidden="true" /> |
0 commit comments