# SharePoint tenant admin site collection URL
$adminSiteUrl = "https://contoso-admin.sharepoint.com"
# SharePoint online Hub site URL
$hubSiteURL = "https://contoso.sharepoint.com/sites/communicationhubsite"
# Array of site collections to associate with hub site
$arrayOfSites = @("https://contoso.sharepoint.com/sites/siteA", "https://contoso.sharepoint.com/sites/siteB", "https://contoso.sharepoint.com/sites/siteC")
# Connect to SharePoint Online admin site
Connect-SPOService -Url $adminSiteUrl
function AssociateHubSite {
try {
foreach ($site in $arrayOfSites) {
Write-Host "Associating site collection: " $site
# Associating site collection with hub site
Add-SPOHubSiteAssociation -Site $site -HubSite $hubSiteURL -ErrorAction Stop
Write-Host "Hub site associated with site collection: " $site -ForegroundColor Green
}
}
catch {
Write-Host "Error in associating hub site $($site): " $_.Exception.Message -ForegroundColor Red
}
# Disconnect SharePoint online connection
Disconnect-SPOService
}
AssociateHubSite
# SharePoint online Hub site URL
$hubSiteURL = "https://contoso.sharepoint.com/sites/communicationhubsite"
# Array of site collections to associate site with hub site
$arrayOfSites = @("https://contoso.sharepoint.com/sites/siteA", "https://contoso.sharepoint.com/sites/siteB", "https://contoso.sharepoint.com/sites/siteC")
# Get Credentials to connect
$m365Status = m365 status
if ($m365Status -match "Logged Out") {
m365 login
}
# Get information about the specified hub site
$hubSiteDetails = m365 spo hubsite get --url $hubSiteURL | ConvertFrom-Json
function AssociateHubSite {
try {
foreach ($site in $arrayOfSites) {
Write-Host "Associating site collection: " $site
# Associating site collection with hub site
m365 spo site hubsite connect --siteUrl $site --id $hubSiteDetails.ID
Write-Host "Hub site associated with site collection: " $site -ForegroundColor Green
}
}
catch {
Write-Host "Error in associating hub site $($site): " $_.Exception.Message -ForegroundColor Red
}
# Disconnect SharePoint online connection
m365 logout
}
AssociateHubSite
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.