Skip to content

Commit 3f89c05

Browse files
authored
Merge pull request pnp#849 from reshmee011/renameHubSiteUrl
Sample script to rename sharePoint Hub site url
2 parents 5d389de + 6bb1a77 commit 3f89c05

File tree

3 files changed

+164
-0
lines changed

3 files changed

+164
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
![Example Screenshot](assets/preview.png)
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" />
Loading
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
[
2+
{
3+
"name": "spo-rename-hub-siteurl",
4+
"source": "pnp",
5+
"title": "Renaming a SharePoint Hub Site URL",
6+
"shortDescription": "How to Safely Rename a SharePoint Hub Site URL",
7+
"url": "https://pnp.github.io/script-samples/spo-rename-hub-siteurl/README.html",
8+
"longDescription": [
9+
"Safely renaming SharePoint Hub Site Url"
10+
],
11+
"creationDateTime": "2025-05-28",
12+
"updateDateTime": "2025-05-28",
13+
"products": [
14+
"SharePoint",
15+
"Sites"
16+
],
17+
"metadata": [
18+
{
19+
"key": "PNP-POWERSHELL",
20+
"value": "3.1.41"
21+
}
22+
],
23+
"categories": [
24+
"SharePoint"
25+
],
26+
"tags": [
27+
"modern",
28+
"Connect-PnPOnline",
29+
"Get-PnPTenantSite",
30+
"Get-PnPHubSite",
31+
"Get-PnPHubSiteChild",
32+
"Unregister-PnPHubSite",
33+
"Rename-PnPTenantSite",
34+
"Remove-PnPTenantSite",
35+
"Register-PnPHubSite"
36+
],
37+
"thumbnails": [
38+
{
39+
"type": "image",
40+
"order": 100,
41+
"url": "https://raw.githubusercontent.com/pnp/script-samples/main/scripts/spo-rename-hub-siteurl/assets/preview.png",
42+
"alt": ""
43+
}
44+
],
45+
"authors": [
46+
{
47+
"gitHubAccount": "reshmee011",
48+
"company": "",
49+
"pictureUrl": "https://avatars.githubusercontent.com/u/7693852?v=4",
50+
"name": "Reshmee Auckloo"
51+
}
52+
],
53+
"references": [
54+
{
55+
"name": "Want to learn more about PnP PowerShell and the cmdlets",
56+
"description": "Check out the PnP PowerShell site to get started and for the reference to the cmdlets.",
57+
"url": "https://aka.ms/pnp/powershell"
58+
}
59+
]
60+
}
61+
]

0 commit comments

Comments
 (0)