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
- $siteURL=Read-Host“Enter the site Url”
- #Enter user name and password
- $credential=Get-Credential
- #connect pnp online
- Connect-PnPOnline-Url$siteURl-Credentials$credential
- #register associate site to hub site
- 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.
Before running the script,
After Running the script,
Associate a site to Hub Site
Follow the below powershell code to associate a site to hubsite.
- $tenantURL=Read-Host“Enter Tenant url”
- $hubURL=Read-Host“Enter Hubsite Url”
- $siteURL=Read-Host“Please provide site Url”
- #enter the user name and password
- $credential=Get-Credential
- #connect-Pnp Online
- Connect-PnPOnline-Url$tenantURL-Credentials$credential
- #add associated site to a hubsite
- 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
After running the script,
Get Associated Site
Follow the below powershell code to get all associated site from a hub site and print it.
- $hubURL=Read-Host“Enter hubsite Url”
- #enter the user name and password
- $credential=Get-Credential
- #Connect Pnp Online
- Connect-PnPOnline-Url$hubURL-Credentials$credential
- #get associated site
- $siteColl=Get-PnPHubSiteChild-Identity$hubURL
- foreach($sitein$siteColl){
- #print the sites
- Write-Host$site
- }
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.
Remove Associated site
Follow the below powershell code to remove associated site
- $tenantURL= Read-Host “provide tenant url”
- $siteURL=Read-Host “provide site url”
- #enter the username and password
- $credential=Get-Credential
- #Connect PnP Online
- Connect-PnPOnline-Url$tenantURL-Credentials$credential
- #remove Hub associated site
- 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,
After running the script,
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.