SharePoint Online Hub Site Association
This script sample shows how to associate SharePoint online site with a hub site.
Summary
This sample shows how to associate sharepoint online site with hub site.
Implementation
- Open Windows PowerShell ISE
- Create a new file
- Write a script as below,
- First, we will connect to Tenant admin site.
- Then we read site url and hub site url from a user,
- And then we will do hub site association.

$AdminSiteURL = "https://{domain}-admin.sharepoint.com/"
$Username = "USERID"
$Password = "********"
$SecureStringPwd = $password | ConvertTo-SecureString -AsPlainText -Force
$Creds = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $secureStringPwd
Function Login() {
[cmdletbinding()]
param([parameter(Mandatory = $true, ValueFromPipeline = $true)] $Creds)
Write-Host "Connecting to Tenant Admin Site '$($adminSiteURL)'" -f Yellow
Connect-PnPOnline -Url $adminSiteURL -Credentials $Creds
Write-Host "Connection Successful" -f Green
}
Function HubSiteAssociation {
try {
#read site url from user
$SiteUrl = Read-Host 'Enter Site Url'
#read hub site url from user
$HubSiteUrl = Read-Host 'Enter Hub Site Url'
Write-Host "Connecting to SharePoint site..." -ForegroundColor Yellow
#connect to the SharePoint site
Connect-PnpOnline -Url $SiteUrl -Credentials $Creds
Write-Host "Associate with a hub site..." -ForegroundColor Yellow
#hub site association
Add-PnPHubSiteAssociation -Site $SiteUrl -HubSite $HubSiteUrl
Write-Host "Hub site association completed..." -ForegroundColor Yellow
}
catch {
Write-Host "Error in hub site association:" $_.Exception.Message -ForegroundColor Red
}
}
Function StartProcessing {
Login($creds);
HubSiteAssociation
}
StartProcessing
$AdminSiteURL = "https://{domain}-admin.sharepoint.com/"
$Username = "USERID"
$Password = "********"
$SecureStringPwd = $password | ConvertTo-SecureString -AsPlainText -Force
$Creds = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $secureStringPwd
Function Login() {
[cmdletbinding()]
param([parameter(Mandatory = $true, ValueFromPipeline = $true)] $Creds)
Write-Host "Connecting to Tenant Admin Site '$($adminSiteURL)'" -f Yellow
Connect-SPOService -Url $adminSiteURL -Credential $Creds
Write-Host "Connection Successful" -f Green
}
Function HubSiteAssociation {
try {
#read site url from user
$SiteUrl = Read-Host 'Enter Site Url'
#read hub site URL from user
$HubSiteUrl = Read-Host 'Enter Hub Site Url'
Write-Host "Connecting to SharePoint site..." -ForegroundColor Yellow
#connect to the SharePoint site
Connect-PnpOnline -Url $SiteUrl -Credentials $Creds
Write-Host "Associate with a hub site..." -ForegroundColor Yellow
#hub site association
Add-SPOHubSiteAssociation $SiteUrl -HubSite $HubSiteUrl
Write-Host "Hub site association completed..." -ForegroundColor Yellow
}
catch {
Write-Host "Error in hub site association:" $_.Exception.Message -ForegroundColor Red
}
}
Function StartProcessing {
Login($Creds);
HubSiteAssociation
}
StartProcessing
Function Login() {
Write-Host "Connecting to Tenant Site" -f Yellow
$m365Status = m365 status
if ($m365Status -match "Logged Out") {
m365 login
}
Write-Host "Connection Successful!" -f Green
}
Function HubSiteAssociation {
try {
#read site url from user
$SiteUrl = Read-Host 'Enter Site Url'
#read hub site url from user
$HubSiteUrl = Read-Host 'Enter Hub Site Url'
#hub site association
Write-Host "Associate with a hub site..." -ForegroundColor Yellow
$hubSitesList = m365 spo hubsite list | ConvertFrom-Json
$hubSite = $hubSitesList | Where-Object SiteUrl -Match $HubSiteUrl
m365 spo hubsite connect --url $SiteUrl --hubSiteId $hubSite.SiteId
Write-Host "Hub site association completed..." -ForegroundColor Yellow
}
catch {
Write-Host "Error in hub site association:" $_.Exception.Message -ForegroundColor Red
}
}
Function StartProcessing {
Login
HubSiteAssociation
}
StartProcessing
Contributors
| Author(s) |
|---|
| Chandani Prajapati (https://github.com/chandaniprajapati) |
| Jasey Waegebaert |
Disclaimer
THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
Tool documentation & references
- Want to learn more about PnP PowerShell and the cmdlets
Check out the PnP PowerShell site to get started and for the reference to the cmdlets.
- Introduction to the SharePoint Online Management Shell
Check out the SPO Management Shell documentation site to get started and for the reference to the cmdlets.
- Want to learn more about CLI for Microsoft 365 and the commands
Check out the CLI for Microsoft 365 site to get started and for the reference to the commands.