SharePoint Online Hub Site Association With PnP Powershell

Introduction

 
Hub Site is a collection of sites connected to a master site from where all connected sites can be navigated and managed.
 
Features
 
Discover related content such as news and other site activities, Apply common navigation, branding, and site structure across associated sites. Search across all associated sites.
 
 
For more details, refer to this article. 
 

Implementation

 
Step 1
 
Open Windows Powershell ISE
 
Step 2
 
Create a new file
 
Step 3
 
Write a script as below,
 
In the script, we will take two inputs from the user
   
Site Url - the site which we want to associate with the hub site
 
Hub site Url - the site URL, with we want to associate our site          
  1. #SharePoint online site connection  
  2. $Login = "[email protected]"  
  3. $password = "********" #Enter your password  
  4. $secureStringPwd = $password | ConvertTo-SecureString -AsPlainText -Force   
  5. $Creds = New-Object -Typename System.Management.Automation.PSCredential -ArgumentList $Login, $secureStringPwd  
  6.   
  7. Function HubSiteAssociation{  
  8.     try {  
  9.         #read site url from user  
  10.         $SiteUrl = Read-Host 'Enter Site Url'  
  11.  
  12.         #read hub site URL from user  
  13.         $HubSiteUrl = Read-Host 'Enter Hub Site Url'         
  14.   
  15.         Write-Host "Connecting to SharePoint site..." -ForegroundColor Yellow  
  16.  
  17.         #connect to the SharePoint site  
  18.         Connect-PnpOnline -Url $SiteUrl -Credentials $Creds     
  19.   
  20.         Write-Host "Association hub site..." -ForegroundColor Yellow  
  21.          
  22.         #hub site association           
  23.         Add-PnPHubSiteAssociation -Site $SiteUrl -HubSite $HubSiteUrl  
  24.   
  25.         Write-Host "Hub site association completed..." -ForegroundColor Yellow  
  26.     }  
  27.     catch {  
  28.         Write-Host "Error in adding hub site association:" $_.Exception.Message -ForegroundColor Red  
  29.     }  
  30. }  
  31.  
  32. #call function  
  33. HubSiteAssociation  
Now run the script with the F5 command.
 
Output
 
Output - Hub site association
 

Summary 

 
In this article, we have seen the association of the SharePoint site with the hub site.
 
I hope this helps.
 
Sharing is caring!