SharePoint Online: Set Hub Site Association using PowerShell
Hub sites in SharePoint Online are a new way to organize related sites together into centralized portals based on organizational hierarchy to share common navigation, branding, and search. More on Hub site: How to Create Hub Sites in SharePoint Online?, This post covers the steps needed to associate a site with a hub site using PowerShell.
Set Hub Site Association in SharePoint Online using PnP PowerShell
To associate a site collection with an existing hub site, use the following script:
#Config Variables
$TenantSiteUrl = "https://crescent-admin.sharepoint.com"
$HubSiteURL = "https://crescent.sharepoint.com/sites/Intranet"
$SiteURL = "https://crescent.sharepoint.com/sites/2020projects"
#Connect to PnP Online
Connect-PnPOnline -Url $TenantSiteUrl -Credentials (Get-Credential)
#Associate Site collection with HubSite
Add-PnPHubSiteAssociation -Site $SiteURL -HubSite $HubSiteURL
Let’s tweak the script bit and add multiple sites to a Hub site:
#Config Variables
$TenantSiteUrl = "https://crescent-admin.sharepoint.com"
$HubSiteURL = "https://crescent.sharepoint.com/sites/Operations"
$SitesToConnect = "https://crescent.sharepoint.com/sites/Ops","https://crescent.sharepoint.com/sites/OpsV2","https://crescent.sharepoint.com/sites/OpsV3"
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $TenantSiteUrl -Interactive
#Get the Hub site
$HubSite = Get-PnPHubSite -Identity $HubSiteURL
#Get all sites connected to the Hub
$SitesConnectedToHub = Get-PnPHubSiteChild -Identity $HubSiteURL
#Associate each Site collection with HubSite
ForEach ($Site in $SitesToConnect)
{
#Check if the site is alreay connected with the Hub
If($SitesConnectedToHub -contains $Site)
{
Write-host "$Site is Already connected to Hub Site" -ForegroundColor Yellow
}
Else
{
Add-PnPHubSiteAssociation -Site $Site -HubSite $HubSiteURL
Write-host "$Site Connected to HUB Site Successfully!" -ForegroundColor Green
}
}
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
Set Hub Site Association for Site Collections from a CSV File:
If you want to associate multiple sites with a hub site, just create a CSV file with Site URLs. Here is mine:
To Associate multiple site collections from a CSV file to a hub site, you can use this script:
#Config Variables
$TenantSiteUrl = "https://crescent-admin.sharepoint.com"
$HubSiteURL = "https://crescent.sharepoint.com/sites/Intranet"
$CSVFilePath = "C:\Temp\Sites.csv"
#Connect to PnP Online
Connect-PnPOnline -Url $TenantSiteUrl -Credentials (Get-Credential)
#Get data from CSV File
$Sites = Import-Csv -Path $CSVFilePath
#Associate each Site collection with HubSite
ForEach ($Site in $Sites)
{
Add-PnPHubSiteAssociation -Site $Site.SiteURL -HubSite $HubSiteURL
Write-host "Associated $($Site.SiteURL) with Hub Site"! -f Green
}
Alright, How do I remove the hub site association from a site collection? Well, to remove the hub site association from a SharePoint Online site collection, use:
Remove-PnPHubSiteAssociation -Site "<Site URL>"
To retrieve all hub sites and their associated sites, use: Get SharePoint Online Hub Site Associations using PowerShell