Share via


SharePoint Online: Hub and connected sites report using PnP PowerShell and Office 365 URL

In this article we will learn about how to retrieve hub sites and associated sites using PnP Powershell and Office 365 admin center url or sharepoint admin center url.

Introduction

Many times being a SharePoint online administrator we need to generate a report of all hub sites and their associated sites or all associated sites for a particular hub site. Dealing with this manually is very much time consuming and there is no straight screen using which we can generate this. So here in this article we will see how we can get all hub sites and their all associated sites, all associated sites for a particular hub site using PnP Powershell. And also we will see using SharePoint online admin center url, how we can generate this report.

PnP PowerShell script to get all sites connected to a hub site

Using the below PnP PowerShell code, we can get the below types of report:

  • Get all associated or connected sites to a particular hub site.
  • Get master report for all sites and their hub sites from the SharePoint online tenant.
CLS
#############################################################################################################################################################
#Description: Using this script we can generate all associated sites for a partuclar hub site and also all hub sites and their associated sites from a tenant.
#Created By: Habibur Rahaman
#Date: 12-22-2019
#############################################################################################################################################################


###################variables section###################################
$userName = "Global-sharepoint2019@globalsharepoint2019.onmicrosoft.com - <Your User name>"
$passWord = "YourPassWord"
$siteURL="https://globalsharepoint2019-admin.sharepoint.com/ <Your sharepoint admin url>"
###################variables section ends here###################################
$encPassWord = convertto-securestring -String $passWord -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $userName, $encPassWord

Connect-PnPOnline -Url $siteURL -Credentials $cred

#Getting the hub site id for which we want to generate the report - those are connected to this hub site.
$hubSiteURL="https://globalsharepoint2019.sharepoint.com/sites/SPHubSite"
$hubSite = Get-PnPTenantSite $hubSiteURL  
$hubSiteId = $hubSite.HubSiteId
write-host " #####Generating sites connected a single hub site report######: " -BackgroundColor DarkGreen
write-host "Hub Site URL: " $hubSiteURL


$associatedSites = @()


#Get all sites associated to the hub site(in the above hub site)
$sitesTenant = Get-PnPTenantSite -Detailed 
$sitesTenant | select url | % {$oneSite = Get-PnPTenantSite $_.url 

  if($oneSite.hubsiteid -eq $hubSiteId)
  {
    
    write-host "Associated Site URL: " $oneSite.url

     $assocatedSiteObject = New-Object PSObject     
     $assocatedSiteObject | Add-Member -MemberType NoteProperty -name "Hub Site URL" -value $hubSiteURL
     $assocatedSiteObject | Add-Member -MemberType NoteProperty -name "Hub Site ID" -value $hubSiteId
     $assocatedSiteObject | Add-Member -MemberType NoteProperty -Name "Associated Site URL" -value $oneSite.Url
     $assocatedSiteObject | Add-Member -MemberType NoteProperty -name "Associated Site Status" -value $oneSite.Status
     

     #Add the object with property to an Array
     $associatedSites += $assocatedSiteObject

  }
}

#Export the site array collection to a CSV file
$associatedSites | Export-CSV "C:\Temp\GetAllSitesAssociatedInHubSites\SitesConnectedToSingleHubSiteReprot.csv" -NoTypeInformation  
write-host " #####Generating sites connected a single hub site report- ends here######: " -BackgroundColor DarkYellow

######The below script will list down all hub sites and their associated connected sites in the tenant.##################
write-host "------------------------------------------------------------------------------------------------------"

write-host " #####Generating master hub sites along with connected sites report for the tenant. ######:" -BackgroundColor DarkGreen

$hubSites=Get-PnPHubSite
$associatedSites = @()
foreach($oneHubSite in $hubSites)
{

   $test=$oneHubSite;
   write-host "Hub Site URL: " $oneHubSite.SiteUrl

   $hubSite = Get-PnPTenantSite $oneHubSite.SiteUrl;  
   $hubSiteId = $hubSite.HubSiteId
 
#Get all sites associated to the hub site(in the above hub site)
$sitesTenant = Get-PnPTenantSite -Detailed 
$sitesTenant | select url | % {$oneSite = Get-PnPTenantSite $_.url 

  if($oneSite.hubsiteid -eq $hubSiteId)
  {
    write-host "Associated Site URL: " $oneSite.url
    $assocatedSiteObject = New-Object PSObject
         
     $assocatedSiteObject | Add-Member -MemberType NoteProperty -name "Hub Site URL" -value $oneHubSite.SiteUrl
     $assocatedSiteObject | Add-Member -MemberType NoteProperty -name "Hub Site ID" -value $oneHubSite.ID
     $assocatedSiteObject | Add-Member -MemberType NoteProperty -Name "Associated Site URL" -value $oneSite.Url
     $assocatedSiteObject | Add-Member -MemberType NoteProperty -name "Associated Site Status" -value $oneSite.Status
     

     #Add the object with property to an Array
     $associatedSites += $assocatedSiteObject

  }
}


}
#Export the site array collection to a CSV file
$associatedSites | Export-CSV "C:\Temp\GetAllSitesAssociatedInHubSites\SitesConnectedToHubSiteReprotForTenant.csv" -NoTypeInformation  
write-host "##### Generating master hub sites along with connected sites report for the tenant ends here ######:" -BackgroundColor DarkYellow

######The below script will list down all hub sites and their associated connected sites in the tenant - ends here##################

Explanation about the hub sites inventory code

This script takes SharePoint online username, password, SharePoint online admin URL and a hub site URL. In order to get all associated sites for a particular hub site, we should know the hub site URL and using the Get-PnPTenantSite command we have to get the id of that hub site which will be used while we are looping thru the all active sites to identify whether that active site of part of this hub site. And finally for whichever active sites "hubsiteid" attribute matches with the "hubsiteid" of hub site what we have passed as a parameter in the beginning - then we export those collection as a csv file.

For a tenant level, hub sites report, we use the same technique but here we don't pass the hub site URL as parameter rather we read all hub sites collection using the Get-PnPHubSite command and all active sites collection using the Get-PnPTenantSite command from a tenant. Then we loop thru first all hub sites then inside the for each loop of Get-PnPHubSite, write one more for each loop for Get-PnPTenantSite collection, then find the "hubsiteid" attribute from hub site and tenant site and wherever match we store in an array object - finally we export that into a csv file, same like the other one.

Prerequisites for the PnP script

Before executing the above code, we must install the PnP in the machine where we will execute the above code. There are many ways to install this - below is the simplest one. 

For PnP PowerShell installation - we need to execute the below command in the PowerShell command window but need to ensure the Powershell version should be more 3.0 in that machine.

(New-Object Net.WebClient).DownloadString('https://raw.githubusercontent.com/sharepoint/PnP-PowerShell/master/Samples/Modules.Install/Install-SharePointPnPPowerShell.ps1')

 Reference URL: Getting started with SharePoint PnP PowerShell

↑ Return to Top

How to execute this script

In order to run the above code successfully, we need to pass the below parameters: 

###################variables section###################################
$userName = "Global-sharepoint2019@globalsharepoint2019.onmicrosoft.com - <Your User name>"
$passWord = "YourPassWord"
$siteURL="https://globalsharepoint2019-admin.sharepoint.com/ <Your SharePoint admin url>"
$hubSiteURL="https://globalsharepoint2019.sharepoint.com/sites/SPHubSite<Hub Site URL>"
###################variables section ends here###################################

PnP PowerShell script to get all sites connected to hub site: Test

Now lets execute the above code.

Lets see the exported hub sites report – how does it look like?

Now we can see the below master report for all hub sites and their associated sites in the SharePoint online tenant:

And we can see the below report for the scenario when we want to get the all associated sites connected to a single hub site.

View all sites those are connected to a hub site using SharePoint admin center url

By now we have learned that how we can generate the hub and their associated sites report using PnP - now we will see how can generate the same report using the thru SharePoint online admin center URL.

Lets follow the below steps to do this:

We need to generate a view to list down the hub site report using the hub site association.

Login to SharePoint admin center url.

Syntax: https://tenant-admin.sharepoint.com

Example: https://globalsharepoint2019-admin.sharepoint.com/

If we navigate to SharePoint online admin center url, we will get the below SharePoint admin center page. Then need to click on “Active Sites” link from left-side panel.

↑ Return to Top

Then from the “Active sites” dashboard click on “Hub” -> Filter by Hub -> then select your hub site – this will list down all sites those are connected to that particular hub site.

For an example – we have selected my hub site “SP Hub Site”

Now – we can see all sites those are connected to the hub site “SP Hub Site”

How to export the hub sites report in excel?

Similarly we can export this report in csv file and we can filter that site report file from the local excel to have a list of all the sites that are part of the same Hub.

To do this we need to follow the below steps:

From the “Active sites” dashboard click on “Export” button. This will export all SharePoint online sites in csv file to your local download folder.

We will get the active sites report as below – from there we can filter as per our need.

Summary: what we had here?

Thus, in this article we have learned the below:

  • Get all sites associated to a particular hub site using PnP PowerShell and SharePoint online URL.
  • Get all hub sites and their associated sites from a tenant using PnP PowerShell and SharePoint online URL.
  • How to use Get-PnPTenantSite command.
  • How to use Get-PnPHubSite.
  • How to export the report in csv file using export-csv command.

Download

The above code can be downloaded from the below two location:

References

See Also

↑ Return to Top