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:
- Login to SharePoint Online Admin Center >> Click on “Active Sites”Â
- Filter based on specific Hub site
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:
#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. 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) #-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
}
}
}
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?
Thanks 🙂
Thank you for awesome script, just one little remark – there is a typo in the PNP scripts in variables:
$HubSietURL –> $HubSiteURL
Cheers!
Thanks for the catch! fixed it.