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.
- #connect to Tenant admin
- Connect - SPOService https: //Tenant-admin.sharepoint.com
- #get all hubsites in the Tenant
- $sites = Get - SPOSite - Limit All
- #get all sites in tenant using the SPOPowershell
- $hubsites = Get - SPOHubSite
- #output array
- $HubSiteassociate = @()
- #file location where CSV will be stored
- $fileLocation = "C:\temp\testscript\HubSiteAssociationReport.csv"
- #iterating
- throw all HubSites
- foreach($h in $hubsites) {
- $hubid = $h.SiteId
- #iterating
- throw all Sites collection
- foreach($site in $sites) {
- #pulling the information
- for indviual site collection
- $sit = Get - SPOSite $site.url
- #checking
- if the Hubsite ID match with Site collection 's HubSite id
- if ($hubid - eq $sit.HubSiteId) {
- $HubSite = New - Object PSObject
- $HubSite | Add - Member - MemberType NoteProperty - name "HUbSite Name" - value $h.Title
- $HubSite | Add - Member - MemberType NoteProperty - Name "Hub Site URL" - value $h.SiteUrl
- $HubSite | Add - Member - MemberType NoteProperty - Name "Assoicated Site Url" - value $sit.url
- $HubSite | Add - Member - MemberType NoteProperty - Name "is it Root Hub Site" - value $sit.IsHubSite
- $HubSiteassociate += $hubsite
- }
- }
- }
- #output the restults
- $HubSiteassociate | Export - Csv $fileLocation - NoTypeInformation
- #disposing
- Disconnect - SPOService

Join the conversation! Your thoughts help the community grow.