SharePoint Online: PowerShell to Get All Site Collections and Subsites

Requirement: SharePoint Online PowerShell to get all site collections and subsites.

SharePoint Online PowerShell to list all site collections and subsites

Do you need to get a list of all sites and subsites in your SharePoint Online environment? PowerShell makes it easy! In this article, we’ll take a look at how to get all site collections and subsites in SharePoint Online using PowerShell from your SharePoint Online tenant. Here is the CSOM PowerShell to list all sites and subsites in SharePoint Online:

Import-Module Microsoft.Online.SharePoint.Powershell

#Config Parameters

#Get Credentials to connect to SharePoint Admin Center
$Cred = Get-Credential

#Function to get all subsites of a site
Function Get-SPOWeb($WebURL)
    #Setup credentials to connect
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Get Web information and subsites
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($webURL)
    $Context.Credentials = $Credentials
    $Web = $context.Web
    #Iterate through each subsite in the current web
    foreach ($Subweb in $web.Webs)
        #Get the web object

        #Call the function recursively to process all subsites underneath the current web

#Connect to SharePoint Online Admin Center
Connect-SPOService -Url $AdminSiteURL -Credential $Cred

#Get All site collections
$SiteCollections = Get-SPOSite -Limit All

#Traverse through each site collection and get their subsits
Foreach ($Site in $SiteCollections)
    Write-Host $Site.Url     
    $AllWebs = Get-SPOWeb $Site.Url
    $AllWebs | ForEach-Object { Write-Host $_.URL }

This gets you the list of site collections and their subsites. Here is another script:

SharePoint Online: PowerShell to List All Subsites in All Site Collections

Let’s get subsites from all site collections from the tenant. Here is the PowerShell script to get all site collections and subsites in SharePoint Online:

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Function to get each subsite in site collection
Function Get-SPOWebs($SiteURL)
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials

    #Get the web
    $Web = $Ctx.Web

    #Do something
    Write-host $Web.Url

    #Loop through each each subsite in site
    Foreach($Web in $web.Webs)
        Get-SPOWebs $Web.Url

$AdminCenterURL = ""

#Setup Credentials to connect
$Cred= Get-Credential
#Connect to SharePoint Online
Connect-SPOService -url $AdminCenterURL -Credential ($Cred)

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

#Loop through site collections
ForEach($Site in $Sites)
    #Call the function to get all sub-sites in site collection

SharePoint Administrators can use this PowerShell to quickly get a list of all sites and subsites in their tenant.

PnP PowerShell to Get All Sites and Subsites in SharePoint Online

Let’s see how to get all sites and subsites in SharePoint Online using PowerShell. This script retrieves all site collections and their subsites recursively using PnP PowerShell.

#Set Variables
$SiteURL = ""

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred

    #Get All Site collections  
    $SitesCollection = Get-PnPTenantSite

    #Loop through each site collection
    ForEach($Site in $SitesCollection)  
        Write-host -F Green $Site.Url  
        Try {
            #Connect to site collection
            Connect-PnPOnline -Url $Site.Url -Credentials $Cred

            #Get Site Collection administrators of the site
            $SubSites = Get-PnPSubWeb -Recurse
            ForEach ($web in $SubSites)
                Write-host `t $Web.URL
        Catch {
            write-host -f Red "`tError:" $_.Exception.Message
Catch {
    write-host -f Red "Error:" $_.Exception.Message

This script gets all sites and subsites in SharePoint Online using PnP PowerShell.

Make sure you have Site collection Administrator rights on all site collections in the tenant before executing this script. Otherwise, you may get “The remote server returned an error: (401) Unauthorized.” error! PowerShell to Add Site Collection Administrator to All Sites in SharePoint Online

Get Subsites Count in Each Site Collection in SharePoint Online:

In another situation, we wanted to quickly get the number of subsites (recursively in total) for each site collection in the tenant.

$TenantAdminURL = ""
#Connect to Admin Center
$Cred = Get-Credential
Connect-PnPOnline -Url $TenantAdminURL -Credentials $Cred
#Get All Site collections - Exclude: Seach Center, Redirect site, Mysite Host, App Catalog, Content Type Hub, eDiscovery and Bot Sites
$SitesCollections = Get-PnPTenantSite | Where -Property Template -NotIn ("SRCHCEN#0","REDIRECTSITE#0", "SPSMSITEHOST#0", "APPCATALOG#0", "POINTPUBLISHINGHUB#0", "EDISC#0", "STS#-1")
#Loop through each site collection
$SubSitesData = @()
ForEach($Site in $SitesCollections)
    #Connect to site collection
    Write-host -f Yellow "Processing site:"$Site.Url    
    Connect-PnPOnline -Url $Site.Url -Credentials $Cred
    #Get the SubSites of the site collection
    $Webs = Get-PnPSubWeb -Recurse

    #Collect Subsites count
    $SubSitesData += New-Object PSObject -Property ([ordered]@{
        SiteName = $Site.Title
        URL = $Site.URL
        SubSitesCount = $Webs.Length

Related Posts:

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

Leave a Reply

Your email address will not be published.