Issue

We want to get a list of all Hub Sites along with their associated sites in tenant using PowerShell. I used the SPO PowerShell but when we loop through all sites collections and get the HubSiteID, it always throws “0000-00-00000-0000” GUID.

Troubleshooting

I tried to get the list using the PNP as well as SPO powerShell but no luck. All I found was that the Get-SPOSite or Get-PNPsite was not loading all the properties so I contacted the SPO team and they confirmed it is by design. They did this due to performance issues.

Resolution

So after getting the answer from them, I was able to successfully write the code for both PNP and SPO to get the list of the all Hub Sites along with their associated sites.
Note
You need minimum SharePoint Tenant Administrator permission to run the below code.

SPO PowerShell

The below script is using the SharePoint Online PowerShell, you need to SharePoint Tenant Admin rights as mentioned before. The script is very simple, first it connects to the SharePoint tenant then gets all sites from the tenant along with the Hub sites. Finally it will itereate throug all the sites and check the HubSite ID and if it matches, it will then save it inside a array. At the end this script will create a CSV file with hubsite name along with all associated sites.
  1. #connect to Tenant admin
  2. Connect - SPOService https: //Tenant-admin.sharepoint.com
  3. #get all hubsites in the Tenant
  4. $sites = Get - SPOSite - Limit All
  5. #get all sites in tenant using the SPOPowershell
  6. $hubsites = Get - SPOHubSite
  7. #output array
  8. $HubSiteassociate = @()
  9. #file location where CSV will be stored
  10. $fileLocation = "C:\temp\testscript\HubSiteAssociationReport.csv"
  11. #iterating
  12. throw all HubSites
  13. foreach($h in $hubsites) {
  14. $hubid = $h.SiteId
  15. #iterating
  16. throw all Sites collection
  17. foreach($site in $sites) {
  18. #pulling the information
  19. for indviual site collection
  20. $sit = Get - SPOSite $site.url
  21. #checking
  22. if the Hubsite ID match with Site collection 's HubSite id
  23. if ($hubid - eq $sit.HubSiteId) {
  24. $HubSite = New - Object PSObject
  25. $HubSite | Add - Member - MemberType NoteProperty - name "HUbSite Name" - value $h.Title
  26. $HubSite | Add - Member - MemberType NoteProperty - Name "Hub Site URL" - value $h.SiteUrl
  27. $HubSite | Add - Member - MemberType NoteProperty - Name "Assoicated Site Url" - value $sit.url
  28. $HubSite | Add - Member - MemberType NoteProperty - Name "is it Root Hub Site" - value $sit.IsHubSite
  29. $HubSiteassociate += $hubsite
  30. }
  31. }
  32. }
  33. #output the restults
  34. $HubSiteassociate | Export - Csv $fileLocation - NoTypeInformation
  35. #disposing
  36. Disconnect - SPOService

SharePoint PNP PowerShell

This script is the same as above but the only difference is instead of Sharepoint Online Powershell I used PNP powershell. Apart from that everything is the same, you need SharePoint tenant admin rights and the latest version of PNP powershell for SharePoint Online installed on you machine.
  1. #connect to Tenant admin
  2. Connect - PnPOnline - Url https: //Tenant-admin.sharepoint.com -UseWebLogin
  3. #get all hubsites in the Tenant
  4. $hubsites = Get - PnPHubSite
  5. #get all sites in tenant using the PNPPowershell
  6. $sites = Get - PnPTenantSite - Detailed
  7. #output array
  8. $HubSiteassociate = @()
  9. #file location where CSV will be stored
  10. $fileLocation = "C:\temp\testscript\PnpHuBsiteReport.csv"
  11. #iterating
  12. throw all HubSites
  13. foreach($hub in $hubsites) {
  14. $hubid = $hub.SiteId
  15. #iterating
  16. throw all Sites collection
  17. foreach($site in $sites) {
  18. #pulling the information
  19. for indviual site collection
  20. $sit = Get - PnPTenantSite $site.url - Detailed
  21. #checking
  22. if the Hubsite ID match with Site collection 's HubSite id
  23. if ($hubid - eq $sit.HubSiteId) {
  24. $HubSite = New - Object PSObject
  25. $HubSite | Add - Member - MemberType NoteProperty - name "HUbSite Name" - value $hub.Title
  26. $HubSite | Add - Member - MemberType NoteProperty - Name "Hub Site URL" - value $hub.SiteUrl
  27. $HubSite | Add - Member - MemberType NoteProperty - Name "Assoicated Site Url" - value $sit.url
  28. $HubSite | Add - Member - MemberType NoteProperty - Name "is it Root Hub Site" - value $sit.IsHubSite
  29. $HubSiteassociate += $hubsite
  30. }
  31. }
  32. }
  33. #output the restults
  34. $HubSiteassociate | Export - Csv $fileLocation - NoTypeInformation
  35. #disposing
  36. Disconnect - PnPOnline

Conculsion

You can save the above script in a PS1 file and run in the powershell console and it will create the csv file on the location mentioned. The above script is very handy when you have many hub sites in your orgnizations and you don't have a report on it. This script also solves the problem of gettting the properties of all the site collections in a tenant. It is a time consuming script but very helpful, you can use it to get any property of all site collections.
See Also