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 = "https://yourtenant-admin.sharepoint.com"
$HubSiteURL = "https://yourtenant.sharepoint.com/sites/YourHubsite"
 
#Connect to PnP Online
Connect-PnPOnline -Url $TenantSiteUrl -UseWebLogin
Get-PnPHubSite -Identity $HubSiteURL
$SitesConnectedToHub = Get-PnPHubSiteChild -Identity $HubSiteURL

$Sites = "https://yourtenant.sharepoint.com/sites/TeamSite1","https://yourtenant.sharepoint.com/sites/TeamSite2","https://yourtenant.sharepoint.com/sites/TeamSite3"

#Associate each Site collection with HubSite
ForEach ($Site in $Sites)
{
    #Write-host " ----------------------------------------------------------"
    if($SitesConnectedToHub.Contains($Site))
    {
        Write-host " == $($Site) Yet connected to Hub Site [$($HubSiteURL)]  ==" -ForegroundColor Yellow
    }
    else
    {
        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