Get Hub Site and Associated Sites in SharePoint Online using PowerShell

SharePoint hub site helps you to organize your intranet by connecting multiple sites in SharePoint Online. In other words, the hub is a collection of sites connected to a master site (Hub site) from where all the associated sites can be navigated and managed. In this article, we will explore how to retrieve hub sites and their connected sites using SPO PowerShell and PnP PowerShell. 

Note: You need SharePoint Tenant administrator or Global administrator permission to get all hub site details.

Summary

Get All Hub Sites and Associated Sites using SPO PowerShell

The below script uses the commands from the SharePoint Online PowerShell module.  The script connects to the SharePoint admin site and gets all site collections and hub sites from the Tenant, then iterates the sites collections one by one to find the associated sites. Finally, the script exports the hub sites and associated sites to a CSV file.

#Provide your SharePoint Online Admin center URL
$AdminSiteURL = "https://contoso-admin.sharepoint.com"
#$AdminSiteURL = "https://<Tenant_Name>-admin.sharepoint.com"
  
#Connect to SharePoint Online
Connect-SPOService -Url $AdminSiteURL

#Get all SPO sites
$Sites = Get-SPOSite -Limit All  
 
#Get all hub sites
$HubSites = Get-SPOHubSite
 
$HubAssociateSites=@() #Result array

#Enumerate the hub sites to get associated sites
ForEach($HubSite in $HubSites)
{
#Get associated sites with hub
$AssociatedSites = $Sites | Where-Object {$_.HubSiteId -eq $HubSite.Id }

#Add hub site in Result array 
$HubAssociateSites += New-Object PSObject -property $([ordered]@{ 
HubSiteName  = $HubSite.Title            
HubSiteURL = $HubSite.SiteUrl
IsHubSite = $true
AssociatedSiteName =$null
AssociatedSiteURL=$null
})
 
if ($AssociatedSites -and $AssociatedSites.Count -gt 0) {
#Enumerate connected sites and add in Result array 
ForEach($AssociatedSite in $AssociatedSites)
{
if($HubSite.SiteUrl -ne $AssociatedSite.Url)
{
$HubAssociateSites += New-Object PSObject -property $([ordered]@{ 
HubSiteName  = $HubSite.Title            
HubSiteURL = $HubSite.SiteUrl
AssociatedSiteName=$AssociatedSite.Title
AssociatedSiteURL=$AssociatedSite.Url
IsHubSite=$false
})
}
}
}
}
#Display hub site and connected sites
$HubAssociateSites | Select HubSiteName, AssociatedSiteName,IsHubSite,HubSiteURL,AssociatedSiteURL

#Export the result to CSV file
$HubAssociateSites | Export-CSV "C:\HubAssociateSites.CSV" -NoTypeInformation -Encoding UTF8

Get Hub and Associated Sites using PnP PowerShell

The below script uses the commands from the PnP PowerShell module to retrieve and export the hub sites and their associated sites.

#Provide your SharePoint Online Admin center URL
$AdminSiteURL = "https://contoso-admin.sharepoint.com"
#$AdminSiteURL = "https://<Tenant_Name>-admin.sharepoint.com"

#Get SharePoint Admin User Credentials  
$Cred = Get-Credential

#Connect to SharePoint Admin Site
Connect-PnPOnline -Url $AdminSiteURL -Credentials $Cred  

#Get all SPO site collections
$Sites = Get-PnPTenantSite -Detailed  
 
#Get all hub sites
$HubSites = Get-PnPHubSite  
 
$HubAssociateSites=@() #Result array

#Enumerate the hub sites to get associated sites
ForEach($HubSite in $HubSites)
{
#Get associated sites with hub
$AssociatedSites = $Sites | Where-Object {$_.HubSiteId -eq $HubSite.Id }

#Add hub site in Result array 
$HubAssociateSites += New-Object PSObject -property $([ordered]@{ 
HubSiteName  = $HubSite.Title            
HubSiteURL = $HubSite.SiteUrl
IsHubSite = $true
AssociatedSiteName =$null
AssociatedSiteURL=$null
})
 
if ($AssociatedSites -and $AssociatedSites.Count -gt 0) {
#Enumerate connected sites and add in Result array 
ForEach($AssociatedSite in $AssociatedSites)
{
if($HubSite.SiteUrl -ne $AssociatedSite.Url)
{
$HubAssociateSites += New-Object PSObject -property $([ordered]@{ 
HubSiteName  = $HubSite.Title            
HubSiteURL = $HubSite.SiteUrl
AssociatedSiteName=$AssociatedSite.Title
AssociatedSiteURL=$AssociatedSite.Url
IsHubSite=$false
})
}
}
}
}
#Display hub site and connected sites
$HubAssociateSites | Select HubSiteName, AssociatedSiteName,IsHubSite,HubSiteURL,AssociatedSiteURL

#Export the result to CSV file
$HubAssociateSites | Export-CSV "C:\HubAssociateSitesPnP.CSV" -NoTypeInformation -Encoding UTF8
Advertisement

Leave a Comment