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:
- #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
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.
- #connect to Tenant admin
- Connect - PnPOnline - Url https:
- #get all hubsites in the Tenant
- $hubsites = Get - PnPHubSite
- #get all sites in tenant using the PNPPowershell
- $sites = Get - PnPTenantSite - Detailed
- #output array
- $HubSiteassociate = @()
- #file location where CSV will be stored
- $fileLocation = "C:\temp\testscript\PnpHuBsiteReport.csv"
- #iterating
- throw all HubSites
- foreach($hub in $hubsites) {
- $hubid = $hub.SiteId
- #iterating
- throw all Sites collection
- foreach($site in $sites) {
- #pulling the information
- for indviual site collection
- $sit = Get - PnPTenantSite $site.url - Detailed
- #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 $hub.Title
- $HubSite | Add - Member - MemberType NoteProperty - Name "Hub Site URL" - value $hub.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 - 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