GitHub Icon Image
GitHub

Associate Multiple Site Collections to Hub Site

Summary

This PowerShell script can be used to associate mutilple site collections to Hub site. You can provide list of site collection URLs in an array.

Implementation

  • Open Windows PowerShell ISE
  • Create a new file
  • Update the parameters with site collection URLs and hub site URL
  • Save the file and run it
  • PnP PowerShell
  • SPO Management Shell
  • CLI for Microsoft 365

# Parameters

# Provide SharePoint online Hub site URL
$HubSiteURL = "https://******.sharepoint.com/sites/**********"

# Array of site collections to associate with hub site
$arrSCs = @("https://******.sharepoint.com/sites/**********", "https://******.sharepoint.com/sites/**********", "https://******.sharepoint.com/sites/**********")

# Get admin user credentials
$creds = (Get-Credential)

function AssociateHubSite {
	try {
		foreach ($SC in $arrSCs) { 
			Write-Host "Connecting site collection: " $SC 
			Connect-PnPOnline -Url $SC -Credentials $creds
			Add-PnPHubSiteAssociation -Site $SC -HubSite $HubSiteURL -ErrorAction Stop
			Write-Host "Hub site associated with site collection: " $SC -ForegroundColor Green            
		}
	}
	catch {
		Write-Host "Error in associating hub site $($SC): " $_.Exception.Message -ForegroundColor Red
	}   
	
	# Disconnect SharePoint online connection
	Disconnect-PnPOnline
}

AssociateHubSite

Check out the PnP PowerShell to learn more at: https://aka.ms/pnp/powershell

The way you login into PnP PowerShell has changed please read PnP Management Shell EntraID app is deleted : what should I do ?


# 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

Check out the SPO Management Shell to learn more at: Introduction SharePoint Online Management Shell | Microsoft Docs


# 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

Check out the CLI for Microsoft 365 to learn more at: https://aka.ms/cli-m365

Important changes coming to the way you login into CLI for Microsoft 365 (effective 9th September 2024) see Changes in PnP Management Shell registration in Microsoft 365

Contributors

Author(s)
Siddharth Vaghasia
Ganesh Sanap

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.

Back to top Script Samples
Generated by DocFX with Material UI