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 organize related sites into centralized portals based on organizational attributes such as projects, teams, concepts, departments, regions, etc. If you are unfamiliar with Hub Sites in Office 365, I’ll encourage you to check out this link before continuing. 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:

  1. Login to SharePoint Online Admin Center >> Click on “Active Sites” 
  2. 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 the list of all hub sites and their associated site collections.

To get associated hub sites of a specific site collection, use the following script:

#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

Here is an example of how to use PnP PowerShell to get all sites associated with a hub site:

#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) #-Interactive

#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 have PowerShell to get all associated site collections with the Hub site.

Get All Hub site associations using the Get-PnPHubSiteChild cmdlet

Let’s list all SharePoint sites joined to the hub using the new PnP PowerShell cmdlet Get-PnPHubSiteChild.

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

#Connect to tenant Admin
Connect-PnPOnline -Url $AdminCenterURL -Interactive

#Get All Sites associated with a Hub
Get-PnPHubSiteChild -Identity $HubSiteURL

Similarly, to get all hub sites and their associations, use:

Get-PnPHubSite | ForEach-Object {
    Write-host -f Yellow "Hub Site '$($_.Title)' URL: $($_.SiteUrl)"
    Get-PnPHubSiteChild -Identity $_.SiteUrl
}

Apply an Action on All Sites Associated with a 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 -Interactive

#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
        }
    }
}

In summary, we explored how to use PowerShell to retrieve SharePoint sites that are joined to a specific hub site. Also, we retrieved all hub sites and their associated child sites with PnP PowerShell!

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

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

  • Hey,

    thanks for the article. I think there is now an even easier way with PnP. (https://pnp.github.io/powershell/cmdlets/Get-PnPHubSiteChild.html)

    Cheers.

    Reply
  • Hi Salaudeen – Is there a script whereby you could copy the Hub site and it’s associated sites to another Sharepoint site and overwrite? Kind of like a poor man’s backup. Thanks!

    Reply
    • You mean, all lists and libraries? Guess You have to use some migration tools for that.

      Reply
      • Therein lies the problem. The migration tool we have (Sharegate) doesn’t support migrating Hub sites and it’s associated sites. Thought maybe there was a way with Powershell to do that. Thanks.

        Reply
  • is there are way to show up all sites with news and show likes and views of all new in one list?

    Reply
  • 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
  • Thanks 🙂

    Reply
  • 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

Your email address will not be published. Required fields are marked *