[Verified] Office 365: Retrieve hub sites and associated sites using PnP Powershell

Detailed Overview of Hub Site in SharePoint Online

4 comments

Loading

In this “Retrieve hub site in SharePoint Online” article, we will learn how to retrieve hub sites and associated sites using PnP Powershell and Office 365 admin center URL or SharePoint admin center URL.

PnP PowerShell script to get all sites connected to a hub site: Hub site in SharePoint online (PowerShell Get sites associated with hub)

  • Using PnP PowerShell get all associated or connected sites to a particular hub site.
  • Get a master report for all sites and their hub sites from the SharePoint online tenant.
CLS
$userName = "Global-sharepoint2019@globalsharepoint2019.onmicrosoft.com"
$passWord = "YourPassWord"
$encPassWord = convertto-securestring -String $passWord -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $userName, $encPassWord

Connect-PnPOnline -Url "globalsharepoint2019-admin.sharepoint.com/" -Credentials $cred

#Getting the hub site id for which we want to generate the report - those are connected to this hub site.
$hubSiteURL="globalsharepoint2019.sharepoint.com/sites/SPHubSite"
$hubSite = Get-PnPTenantSite $hubSiteURL  
$hubSiteId = $hubSite.HubSiteId
write-host " #####Generating sites connected a single hub site report######: " -BackgroundColor DarkGreen
write-host "Hub Site URL: " $hubSiteURL


$associatedSites = @()


#Get all sites associated to the hub site(in the above hub site)
$sitesTenant = Get-PnPTenantSite -Detailed 
$sitesTenant | select url | % {$oneSite = Get-PnPTenantSite $_.url 

  if($oneSite.hubsiteid -eq $hubSiteId)
  {
    
    write-host "Associated Site URL: " $oneSite.url

     $assocatedSiteObject = New-Object PSObject     
     $assocatedSiteObject | Add-Member -MemberType NoteProperty -name "Hub Site URL" -value $hubSiteURL
     $assocatedSiteObject | Add-Member -MemberType NoteProperty -name "Hub Site ID" -value $hubSiteId
     $assocatedSiteObject | Add-Member -MemberType NoteProperty -Name "Associated Site URL" -value $oneSite.Url
     $assocatedSiteObject | Add-Member -MemberType NoteProperty -name "Associated Site Status" -value $oneSite.Status
     

     #Add the object with property to an Array
     $associatedSites += $assocatedSiteObject

  }
}

#Export the site array collection to a CSV file
$associatedSites | Export-CSV "C:\Temp\GetAllSitesAssociatedInHubSites\SitesConnectedToSingleHubSiteReprot.csv" -NoTypeInformation  
write-host " #####Generating sites connected a single hub site report- ends here######: " -BackgroundColor DarkYellow

######The below script will list down all hub sites and their associated connected sites in the tenant.##################
write-host "------------------------------------------------------------------------------------------------------"

write-host " #####Generating master hub sites along with connected sites report for the tenant. ######:" -BackgroundColor DarkGreen

$hubSites=Get-PnPHubSite
$associatedSites = @()
foreach($oneHubSite in $hubSites)
{

   $test=$oneHubSite;
   write-host "Hub Site URL: " $oneHubSite.SiteUrl


   $hubSite = Get-PnPTenantSite $oneHubSite.SiteUrl;  
   $hubSiteId = $hubSite.HubSiteId


 


#Get all sites associated to the hub site(in the above hub site)
$sitesTenant = Get-PnPTenantSite -Detailed 
$sitesTenant | select url | % {$oneSite = Get-PnPTenantSite $_.url 

  if($oneSite.hubsiteid -eq $hubSiteId)
  {
    write-host "Associated Site URL: " $oneSite.url
    $assocatedSiteObject = New-Object PSObject
         
     $assocatedSiteObject | Add-Member -MemberType NoteProperty -name "Hub Site URL" -value $oneHubSite.SiteUrl
     $assocatedSiteObject | Add-Member -MemberType NoteProperty -name "Hub Site ID" -value $oneHubSite.ID
     $assocatedSiteObject | Add-Member -MemberType NoteProperty -Name "Associated Site URL" -value $oneSite.Url
     $assocatedSiteObject | Add-Member -MemberType NoteProperty -name "Associated Site Status" -value $oneSite.Status
     

     #Add the object with property to an Array
     $associatedSites += $assocatedSiteObject

  }
}


}
#Export the site array collection to a CSV file
$associatedSites | Export-CSV "C:\Temp\GetAllSitesAssociatedInHubSites\SitesConnectedToHubSiteReprotForTenant.csv" -NoTypeInformation  
write-host "##### Generating master hub sites along with connected sites report for the tenant ends here ######:" -BackgroundColor DarkYellow

######The below script will list down all hub sites and their associated connected sites in the tenant - ends here##################

 

PnP PowerShell script to get all sites connected to hub site: Retrieve Hub Test

PnP PowerShell script to get all sites connected to hub site
PnP PowerShell script to get all sites connected to the hub site

Let’s see the exported hub sites report – how does it look like?

Master report for all hub sites in the SharePoint Online tenant:

Hub site report in csv using PnP PowerShell script

Hub site association – Retrieve Hub

All associated sites connected to a single hub site report.

Hub site report in csv using PnP PowerShell script - connected to a single to hub site
PnP PowerShell script to get all sites connected to the hub site

Retrieve Hub

View all sites that are connected to a hub site using the SharePoint admin center URL: hub site association

We need to generate a view to list down the hub site report using the hub site association.

Login to the SharePoint admin center URL.

Syntax:

tenant-admin.sharepoint.com

Example:

globalsharepoint2019-admin.sharepoint.com/

If we navigate to the SharePoint Online admin center URL, we will get the below SharePoint admin center page. Then, click on the “Active Sites” link from the left-side panel.

SharePoint Admin center in SharePoint online hub site association
SharePoint Online hub site association

Then from the “Active sites” dashboard click on “Hub” -> Filter by Hub -> then select your hub site – this will list down all sites that are connected to that particular hub site.

For example – I have selected my hub site “SP Hub Site”

Active sites report in SharePoint online for hub site association
SharePoint Online hub site association

Now – we can see all sites that are connected to the hub site “SP Hub Site”

Hub sites and associated sites dashboard in active sites report

Similarly, we can export this report in a CSV file and we can filter that site report file from the local excel to have a list of all the sites that are part of the same Hub.

To do this we need to follow the below steps:

From the “Active sites” dashboard click on the “Export” button. This will export all SharePoint online sites in the CSV file to your local download folder.

Export active sites to csv in SharePoint online hub site association

Demo: PowerShell Get sites associated with hub (Retrieve Hub)

We will get the active sites report as below – from there we can filter as per our need.

Exported active sites report in csv file

What’s New in SharePoint Online Hub Sites in 2024

In 2024, SharePoint Online has introduced several updates and enhancements to hub sites, aimed at improving user experience, customization, and content management.

Enhanced Customization and Design

  1. Site Scripting during Hub Site Association: When a site is associated with a hub site, it now inherits the theme and can be further configured using site designs and site scripts. This allows for adjustments in permissions, application of shared metadata, and enforcement of content management policies​ (SharePoint hub sites updates, plus new page and web part improvements)​.
  2. Header Emphasis: There’s a new option to modify the color of the site header, providing more flexibility in site theming. This feature allows site owners to choose lighter or darker colors based on the current site theme​​.

Improved Content Management

  1. News Links: Users can now share links to external news articles and other content, integrating them seamlessly into the SharePoint news service. This feature supports adding images, titles, and descriptions, enhancing the visibility of external content within the organization​.
  2. New Web Part Layout Options: Various web parts, including Quick Links, Image Gallery, Hero, and News, have received new layout options. These enhancements help in creating more engaging and visually appealing pages​​.

Enhanced Collaboration and Administration

  1. Graph API for SharePoint Pages: The Microsoft Graph API for SharePoint pages is now generally available. This allows developers to programmatically interact with SharePoint Pages and News posts, offering capabilities such as listing, creating, editing, and deleting pages​.
  2. Version History Controls: Improved version history controls allow admins to set appropriate levels of recoverability and auditability, optimize version recovery and storage, and report on version storage usage across sites​​.

These updates provide more flexibility, better content management, and enhanced collaboration capabilities, making SharePoint Online hub sites more powerful and user-friendly in 2024.

Summary: PowerShell Get sites associated with hub (get hub site in SharePoint Online)

Thus, in this article we have learned the below:

  • Get all sites associated with a SharePoint Online Hub Site – using SharePoint Online Admin center URL.
  • Get all sites associated with a SharePoint Online Hub Site using PnP PowerShell.
  • How to retrieve hub sites and associated sites using PnP Powershell.
  • How to use the Get-PnPTenantSite command.
  • Create a hub site in SharePoint Online.
  • How to use Get-PnPHubSite.
  • How to export the report in CSV file using the export-CSV command.

See Also: SharePoint Online tutorial

You may also like the below SharePoint Online tutorials:

 

About Post Author

4 comments on “Detailed Overview of Hub Site in SharePoint Online”

Do you have a better solution or question on this topic? Please leave a comment