Introduction

Often times, as a SharePoint online administrator we must generate a report of all hub sites and their associated sites or all associated sites for a particular hub site. Dealing with this manually is very time consuming and there is no straight screen using which can generate this. Here in this article, we will see how we can get all hub sites and their all associated sites for a particular hub site using PnP Powershell. Also, we will see how we can generate this report using a SharePoint online admin center URL.

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

Using the below PnP PowerShell code, we can get the noted types of reports:
  • 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.
  1. CLS
  2. #############################################################################################################################################################
  3. #Description: Using this script we can generate all associated sites for a partuclar hub site and also all hub sites and their associated sites from a tenant.
  4. #Created By: Habibur Rahaman
  5. #Date: 12-22-2019
  6. #############################################################################################################################################################
  7. ###################variables section###################################
  8. $userName = "[email protected] - <Your User name>"
  9. $passWord = "YourPassWord"
  10. $siteURL="https://globalsharepoint2019-admin.sharepoint.com/ <Your sharepoint admin url>"
  11. ###################variables section ends here###################################
  12. $encPassWord = convertto-securestring -String $passWord -AsPlainText -Force
  13. $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $userName, $encPassWord
  14. Connect-PnPOnline -Url $siteURL -Credentials $cred
  15. #Getting the hub site id for which we want to generate the report - those are connected to this hub site.
  16. $hubSiteURL="https://globalsharepoint2019.sharepoint.com/sites/SPHubSite"
  17. $hubSite = Get-PnPTenantSite $hubSiteURL
  18. $hubSiteId = $hubSite.HubSiteId
  19. write-host " #####Generating sites connected a single hub site report######: " -BackgroundColor DarkGreen
  20. write-host "Hub Site URL: " $hubSiteURL
  21. $associatedSites = @()
  22. #Get all sites associated to the hub site(in the above hub site)
  23. $sitesTenant = Get-PnPTenantSite -Detailed
  24. $sitesTenant | select url | % {$oneSite = Get-PnPTenantSite $_.url
  25. if($oneSite.hubsiteid -eq $hubSiteId)
  26. {
  27. write-host "Associated Site URL: " $oneSite.url
  28. $assocatedSiteObject = New-Object PSObject
  29. $assocatedSiteObject | Add-Member -MemberType NoteProperty -name "Hub Site URL" -value $hubSiteURL
  30. $assocatedSiteObject | Add-Member -MemberType NoteProperty -name "Hub Site ID" -value $hubSiteId
  31. $assocatedSiteObject | Add-Member -MemberType NoteProperty -Name "Associated Site URL" -value $oneSite.Url
  32. $assocatedSiteObject | Add-Member -MemberType NoteProperty -name "Associated Site Status" -value $oneSite.Status
  33. #Add the object with property to an Array
  34. $associatedSites += $assocatedSiteObject
  35. }
  36. }
  37. #Export the site array collection to a CSV file
  38. $associatedSites | Export-CSV "C:\Temp\GetAllSitesAssociatedInHubSites\SitesConnectedToSingleHubSiteReprot.csv" -NoTypeInformation
  39. write-host " #####Generating sites connected a single hub site report- ends here######: " -BackgroundColor DarkYellow
  40. ######The below script will list down all hub sites and their associated connected sites in the tenant.##################
  41. write-host "------------------------------------------------------------------------------------------------------"
  42. write-host " #####Generating master hub sites along with connected sites report for the tenant. ######:" -BackgroundColor DarkGreen
  43. $hubSites=Get-PnPHubSite
  44. $associatedSites = @()
  45. foreach($oneHubSite in $hubSites)
  46. {
  47. $test=$oneHubSite;
  48. write-host "Hub Site URL: " $oneHubSite.SiteUrl
  49. $hubSite = Get-PnPTenantSite $oneHubSite.SiteUrl;
  50. $hubSiteId = $hubSite.HubSiteId
  51. #Get all sites associated to the hub site(in the above hub site)
  52. $sitesTenant = Get-PnPTenantSite -Detailed
  53. $sitesTenant | select url | % {$oneSite = Get-PnPTenantSite $_.url
  54. if($oneSite.hubsiteid -eq $hubSiteId)
  55. {
  56. write-host "Associated Site URL: " $oneSite.url
  57. $assocatedSiteObject = New-Object PSObject
  58. $assocatedSiteObject | Add-Member -MemberType NoteProperty -name "Hub Site URL" -value $oneHubSite.SiteUrl
  59. $assocatedSiteObject | Add-Member -MemberType NoteProperty -name "Hub Site ID" -value $oneHubSite.ID
  60. $assocatedSiteObject | Add-Member -MemberType NoteProperty -Name "Associated Site URL" -value $oneSite.Url
  61. $assocatedSiteObject | Add-Member -MemberType NoteProperty -name "Associated Site Status" -value $oneSite.Status
  62. #Add the object with property to an Array
  63. $associatedSites += $assocatedSiteObject
  64. }
  65. }
  66. }
  67. #Export the site array collection to a CSV file
  68. $associatedSites | Export-CSV "C:\Temp\GetAllSitesAssociatedInHubSites\SitesConnectedToHubSiteReprotForTenant.csv" -NoTypeInformation
  69. write-host "##### Generating master hub sites along with connected sites report for the tenant ends here ######:" -BackgroundColor DarkYellow
  70. ######The below script will list down all hub sites and their associated connected sites in the tenant - ends here##################

Explanation about the hub sites inventory code

This script takes a SharePoint online username, password, SharePoint online admin URL and a hub site URL. In order to get all associated sites for a particular hub site, we should know the hub site URL and using the Get-PnPTenantSite command we have to get the id of that hub site which will be used while we are looping thru the all active sites to identify whether that active site of part of this hub site. And finally for whichever active sites "hubsiteid" attribute matches with the "hubsiteid" of hub site what we have passed as a parameter in the beginning - then we export the collection as a CSV file.
For a tenant level, hub sites report, we use the same technique but here we don't pass the hub site URL as a parameter. Rather, we read all hub sites collection using the Get-PnPHubSite command and all active sites collection using the Get-PnPTenantSite command from a tenant. Then we loop thru first all hub sites then inside the for each loop of Get-PnPHubSite, write one more for each loop for Get-PnPTenantSite collection, then find the "hubsiteid" attribute from hub site and tenant site and wherever match we store in an array object - finally we export that into a CSV file, the same as the other one.

Prerequisites for the PnP script

Before executing the above code, we must install the PnP in the machine where we will execute the above code. There are many ways to install this - below is the simplest one.
For PnP PowerShell installation - we need to execute the below command in the PowerShell command window but need to ensure the Powershell version should be more 3.0 in that machine.
  1. (New-Object Net.WebClient).DownloadString('https://raw.githubusercontent.com/sharepoint/PnP-PowerShell/master/Samples/Modules.Install/Install-SharePointPnPPowerShell.ps1')

How to execute this script

In order to run the above code successfully, we need to pass the below parameters,
  1. ###################variables section###################################
  2. $userName = "[email protected] - <Your User name>"
  3. $passWord = "YourPassWord"
  4. $siteURL="https://globalsharepoint2019-admin.sharepoint.com/ <Your SharePoint admin url>"
  5. $hubSiteURL="https://globalsharepoint2019.sharepoint.com/sites/SPHubSite<Hub Site URL>"
  6. ###################variables section ends here###################################

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

Now, let's execute the above code.
Hub Sites Report In SharePoint Online Using PnP
Let's see the exported hub sites report – what does it look like?
Now we can see the below master report for all hub sites and their associated sites in the SharePoint online tenant,
Hub Sites Report In SharePoint Online Using PnP
And we can see the below report for the scenario when we want to get the all associated sites connected to a single hub site.
Hub Sites Report In SharePoint Online Using PnP

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

By now we have learned that how we can generate the hub and their associated sites report using PnP - now we will see how we can generate the same report using the thru SharePoint online admin center URL.
Let's follow the below steps to do this.
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: https://tenant-admin.sharepoint.com
Example: https://globalsharepoint2019-admin.sharepoint.com/
If we navigate to SharePoint online admin center URL, we will get the below SharePoint admin center page. Then need to click on "Active Sites" link from the left-side panel.
Hub Sites Report In SharePoint Online Using PnP
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, we have selected my hub site "SP Hub Site"
Hub Sites Report In SharePoint Online Using PnP
Now we can see all sites that are connected to the hub site "SP Hub Site"
Hub Sites Report In SharePoint Online Using PnP

How to export the hub sites report in Excel?

Similarly, we can export this report in 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.
Hub Sites Report In SharePoint Online Using PnP
We will get the active sites report as below – from there we can filter as per our need.
Hub Sites Report In SharePoint Online Using PnP

Download

Summary

In this article, we learned the below concepts with respect to hub sites report in SharePoint online:
  • Get all sites associated with a particular hub site using PnP PowerShell and SharePoint online URL.
  • Get all hub sites and their associated sites from a tenant using PnP PowerShell and SharePoint online URL.
  • How to use the Get-PnPTenantSite command.
  • How to use Get-PnPHubSite.
  • How to export the report in a CSV file using the export-csv command.