Configure

Associate Multiple Site Collections to Hub Site

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.

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
powershell

# 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

Contributors

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

Keep exploring