SharePoint - PowerShell script to Set Hub Association in SharePoint Online

Steel Contributor

Dear all,

Based on this script published:

And the PNP resources provided for Hub Site:

I created this script simply use the list of provide site URL to check if there is yet a Hub configured, if not it's assigning the provide Hub Site URL address as connected.

#Config Variables
$TenantSiteUrl = ""
$HubSiteURL = ""
#Connect to PnP Online
Connect-PnPOnline -Url $TenantSiteUrl -UseWebLogin
Get-PnPHubSite -Identity $HubSiteURL
$SitesConnectedToHub = Get-PnPHubSiteChild -Identity $HubSiteURL

$Sites = "","",""

#Associate each Site collection with HubSite
ForEach ($Site in $Sites)
    #Write-host " ----------------------------------------------------------"
        Write-host " == $($Site) Yet connected to Hub Site [$($HubSiteURL)]  ==" -ForegroundColor Yellow
        Write-host " == $($Site) Need to be connected to Hub Site [$($HubSiteURL)]  ==" -ForegroundColor Red
        Add-PnPHubSiteAssociation -Site $Site -HubSite $HubSiteURL
        Write-host "      ==> $($Site) Connected to HUB Site !" -ForegroundColor Green


You only have to configure the correct URLs and run it with SharePoint Admin permission set on your tenant.


Fabrice Romelard

0 Replies