Get, Set And Remove Associated Site Using PowerShell

In this blog, we are going to discuss about how to create Hub site, associate a site to it, get the associated site and remove the associated site using PowerShell.
 

What is Hub Site?

 
Hub sites bring the related sites together to roll up news, events & to simplify the search with shared navigation across the sites. These sites are meant to fulfil all the organisational need depending upon the projects and departments etc. A hub site can have team sites and communication sites associated to it. You can easily set related sites with shared look and feel, shared global navigation, scoped search and news and activates.
 
Follow the below steps to perform hub site operations using PnP PowerShell.
  • Register a site as Hub.
  • Associate a site to Hub Site.
  • Get Associated Site of Hub site
  • Remove Associated site

Register a site as Hub

 
Follow the below code to register a site as hub
  1. $siteURL=Read-Host“Enter the site Url”  
  2. #Enter user name and password  
  3. $credential=Get-Credential  
  4. #connect pnp online  
  5. Connect-PnPOnline-Url$siteURl-Credentials$credential  
  6. #register associate site to hub site  
  7. Register-PnPHubSite-Site$siteURL   
In the parameter of “siteURL”, Enter that site URL which you want to register as Hub.
 
A Credential pop up will open as shown below. Enter your SharePoint username and password.
 
Get, Set and Remove Associated site using PowerShell
 
Before running the script,
 
Get, Set and Remove Associated site using PowerShell
 
After Running the script,
 
Get, Set and Remove Associated site using PowerShell
 

Associate a site to Hub Site

 
Follow the below powershell code to associate a site to hubsite.
  1. $tenantURL=Read-Host“Enter Tenant url”  
  2. $hubURL=Read-Host“Enter Hubsite Url”  
  3. $siteURL=Read-Host“Please provide site Url”  
  4. #enter the user name and password  
  5. $credential=Get-Credential  
  6. #connect-Pnp Online  
  7. Connect-PnPOnline-Url$tenantURL-Credentials$credential  
  8. #add associated site to a hubsite  
  9. Add-PnPHubSiteAssociation-Site$siteURL-HubSite$hubURL   
In the parameter of “tenantURl”,enter your admin url like https://cottoso-admin.sharepoint.com.
 
In “hubURL” parameter, enter your hub site url in which you want to associate a site.
 
In “siteURL”, provide the site url that you want to associate to hub site.
 
Before running the script
 
Get, Set and Remove Associated site using PowerShell
 
After running the script,
 
Get, Set and Remove Associated site using PowerShell
 

Get Associated Site

 
Follow the below powershell code to get all associated site from a hub site and print it.
  1. $hubURL=Read-Host“Enter hubsite Url”  
  2. #enter the user name and password  
  3. $credential=Get-Credential  
  4. #Connect Pnp Online  
  5. Connect-PnPOnline-Url$hubURL-Credentials$credential  
  6. #get associated site  
  7. $siteColl=Get-PnPHubSiteChild-Identity$hubURL  
  8. foreach($sitein$siteColl){  
  9.    #print the sites  
  10.    Write-Host$site  
  11. }   
In the parameter of “hubURL”,enter your hub site url from which you want to get all the associate site.
 
You can see your associated site url in the console as shown in below screenshot.
 
Get, Set and Remove Associated site using PowerShell
 

Remove Associated site

 
Follow the below powershell code to remove associated site
  1. $tenantURL= Read-Host “provide tenant url”  
  2. $siteURL=Read-Host “provide site url”  
  3. #enter the username and password  
  4. $credential=Get-Credential  
  5. #Connect PnP Online  
  6. Connect-PnPOnline-Url$tenantURL-Credentials$credential  
  7. #remove Hub associated site  
  8. Remove-PnPHubSiteAssociation-Site$siteURL   
In the parameter of “tenantURl”,enter your admin url like https://cottoso-admin.sharepoint.com
 
In “siteURL”, provide the site url that you want to remove from association to hub site.
 
Before running the script,
 
Get, Set and Remove Associated site using PowerShell
 
After running the script,
 
Get, Set and Remove Associated site using PowerShell
 

Conclusion

 
After running the above piece of code I hope it takes less time in comparison to the manual procedure. Hence using the PowerShell script to create hub site, associate a site to it, get the associated site and remove the associated site is more effective procedure.