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:

sharepoint online associate hub site powershell

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

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

2 thoughts on “SharePoint Online: Set Hub Site Association using PowerShell

  • If the sites have existing association to another hub, will this remove the association and force the new hub connection?

    Reply
    • It simply removes the association from existing Hub and associates with the new Hub!

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *