PowerShell to Get All Sites Associated with the Hub Sites in SharePoint Online

Requirement: Get hub site association in SharePoint Online using PowerShell

SharePoint Online: Get Hub Sites Association using PowerShell

SharePoint Online Hub sites help to organize related sites together into centralized portals based on organizational attributes such as projects, teams, concepts, departments, regions, etc. Hub Sites in SharePoint Online are explained in my other article: Configuring Hub Sites in SharePoint Online

How to Get All Hub-Sites and Its Associated Site Collections?

To get all hub site associations, head on to:

  • SharePoint Online Admin Center >> Click on “Active Sites” 
  • Filter based on specific Hub site
    sharepoint online get all hubsites associated

This gives you the list of sites associated with the particular hub site.


PowerShell to Get Hub-Site Associations in SharePoint Online:

Here is the PowerShell to get all sites associated with the hub sites in SharePoint Online

#Import SharePoint Online PowerShell Module
Import-Module Microsoft.Online.SharePoint.Powershell -DisableNameChecking

#Variables
$AdminCenterURL = "https://crescent-admin.sharepoint.com"
 
#Connect to SharePoint Online
Connect-SPOService -Url $AdminCenterURL -Credential (Get-Credential)

#Get all Hubsites
$HubSites = Get-SPOHubSite

#Get all site collections
$Sites = Get-SPOSite -Limit All  

#Iterate through site collections to get associated sites with the hub
ForEach($Hub in $HubSites)
{
    Write-host "Hub-Site:"$Hub.SiteUrl
    #Get the ID of the Hubsite
    $HubSiteId = $Hub.ID.GUID

    #Iterate through each site collection
    ForEach($Site in $Sites)
    {
        #get the Associated Hubsite ID
        $AssociatedHubSiteID = (Get-SPOSite $site.Url).HubSiteID.GUID

        If($AssociatedHubSiteID -eq $HubSiteId)
        {
            Write-Host "`t $($Site.Url)"
        }
    }
}

This PowerShell script gets you list of all hub sites and their associated site collections. To get associated hub sites of a specific site collection, use:

#Import SharePoint Online PowerShell Module
Import-Module Microsoft.Online.SharePoint.Powershell -DisableNameChecking

#Variables
$AdminCenterURL = "https://crescent-admin.sharepoint.com"
$HubSiteURL = "https://crescent.sharepoint.com/sites/intranet"
 
#Connect to SharePoint Online
Connect-SPOService -Url $AdminCenterURL -Credential (Get-Credential)

#Get the Hubsite ID
$HubSiteId = (Get-SPOHubSite | Where {$_.SiteUrl -eq $HubSiteURL}).Id.Guid

#Get all site collections
$Sites = Get-SPOSite -Limit All  

#Iterate through site collections to get associated sites with the hub
ForEach($Site in $Sites)
{
    Try {
        #Get the Associated Hubsite ID of the site collection
        $AssociatedHubSiteID = (Get-SPOSite $site.Url).HubSiteID.GUID

        If($AssociatedHubSiteID -eq $HubSiteId)
        {
            Write-Host "`t $($Site.Url)"
        }
    }
    Catch {
        write-host -f Red "`tError:" $_.Exception.Message
    }
}


Get All Sites Associated with a Hub Site using PnP PowerShell

#Variables
$AdminCenterURL = "https://crescent-admin.sharepoint.com"
$HubSiteURL = "https://crescent.sharepoint.com/sites/intranet"

#Connect to Pnp Online
Connect-PnPOnline -Url $AdminCenterURL  -Credentials (Get-Credential) #-UseWebLogin

#Get the hub site id
$HubSiteID = (Get-PnPTenantSite $HubSiteURL).HubSiteId

#Get all site collections associated with the hub site
Get-PnPTenantSite -Detailed | select url | ForEach-Object { 
  $Site = Get-PnPTenantSite $_.url 
  If($Site.HubSiteId -eq $HubSiteId){
    write-host $Site.url
  }
}

We’ve the PowerShell to get all associated site collections with Hub site. Now, let’s apply it in action by setting all associated sites to read-only mode!

#Variables
$AdminCenterURL = "https://crescent-admin.sharepoint.com"
$HubSiteURL = "https://crescent.sharepoint.com/sites/intranet"

#Connect to Pnp Online
Connect-PnPOnline -Url $AdminCenterURL  -Credentials (Get-Credential) #-UseWebLogin

#Get the hub site id
$HubSiteID = (Get-PnPTenantSite $HubSiteURL).HubSiteId

#Loop through site collections
$Sites  = Get-PnPTenantSite
ForEach($Site in $Sites)
{
    #Get the Site collection
    $Site = Get-PnPTenantSite $Site.url -ErrorAction SilentlyContinue
    If($Site)
    { 
        If($Site.HubSiteId -eq $HubSiteId)
        {
            write-host "Setting Site Collection to Read-only:"$Site.url
            Set-PnPTenantSite -Url $Site.URL -LockState ReadOnly
        }
    }
}

Salaudeen Rajack

Information Technology Professional with Two decades of SharePoint Experience.

4 thoughts on “PowerShell to Get All Sites Associated with the Hub Sites in SharePoint Online

  • May 24, 2021 at 2:16 AM

    Thank you for all the articles, it really helped me understand PnP Powershell. Could you please show how can we export a csv. list of all folders from all Sites associated with a Hub Site?

    Reply
  • September 4, 2020 at 10:36 AM

    Thank you for awesome script, just one little remark – there is a typo in the PNP scripts in variables:
    $HubSietURL –> $HubSiteURL

    Cheers!

    Reply

Leave a Reply