SharePoint Online: PowerShell to Get All Subsites in a Site Collection

Requirement: PowerShell to list all subsites in a SharePoint Online Site collection.

sharepoint online powershell get all subsites

How to Get a List of Subsites in a SharePoint Online site?

There are a few different ways that you can list all subsites in a site collection in SharePoint Online. One option is to use the site contents page.

  1. Navigate to the site >> Click on settings >> Choose “Site Contents”.
  2. In the site contents page, click on “Subsites” (URL shortcut: /_layouts/15/viewlsts.aspx?view=15). This page will display a list of all subsites in the current site collection.
    list all subsites in a sharepoint online site

However, this page lists only the immediate subsites of the site! If there are any 2nd-level subsites, you won’t get them on this page.

SharePoint Online: Get All Subsites using PowerShell

How to get all subsites of a site collection in SharePoint Online? As a SharePoint administrator, you may need to get a list of all subsites in your SharePoint Online environment, which can be done using PowerShell. This blog post will show you how to use PowerShell to get a list of all subsites in your SharePoint Online site.

To get all subsites of a site (either a top-level site or sub-site), use this PowerShell script:

#Requirement: sharepoint online get all subsites powershell
#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"

#Variables
$SiteURL = "https://crescent.sharepoint.com/Sites/Marketing"

Try {
    #Get Credentials to connect
    $Cred= Get-Credential

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
    #Get the web
    $Web = $Ctx.web
    $Ctx.Load($Web)
    
    #get all subsites in sharepoint online powershell
    $Ctx.Load($Web.Webs)
    $Ctx.executeQuery()

    #Loop through each subsite and get subsite URL
    ForEach ($Subweb in $web.Webs)  
    {
       Write-host $Subweb.url  
    }  
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

This PowerShell script gets all immediate subsites (or 1st level subsites) of a given site. How to list all subsites recursively?

PowerShell to Get all sub-sites in a Site Collection

Here is the SharePoint Online PowerShell to get all subsites (Webs) recursively:

#Load SharePoint Online 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"
  
##Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/Sales/"
$UserName="Salaudeen@crescent.com"
$Password ="Password goes here"
 
#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
 
#sharepoint online powershell get subsites
Function Get-SPOWeb() {
param(
    $WebURL = $(throw "Please Enter the Site Collection URL")
)

    #Get Web information and subsites
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($webURL)
    $Context.Credentials = $credentials
    $Web = $context.Web
    $Context.Load($web)

    #powershell cmdlet to retrieve sharepoint online subsites
    $Context.Load($web.Webs) 
    $Context.executeQuery()

    #Do something with the current sub-site
    Write-host $Web.URL

    #Iterate through each subsite in the current web
    foreach ($Subweb in $web.Webs)
    {
        #Call the function recursively to process all subsites underneath the current web
        Get-SPOWeb($Subweb.url)
    }
}

#Call the function
Get-SPOWeb -WebURL $SiteUrl

This PowerShell gets all subsites from a given site collection. This can be useful if you need a report on all the subsites in a site collection or if you need to perform some action on all of them at once. E.g., iterate through all subsites and change the logo: SharePoint Online: How to Change Site Logo using PowerShell?

Get Subsites of a Site using PowerShell PnP cmdlet Get-PnPSubWeb

We can get Subsites in SharePoint Online using PowerShell Pnp as well. Here is the PnP PowerShell to get all subsites in a site collection in SharePoint Online.

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"

#Get Credentials to connect
$Cred = Get-Credential

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

    #Get subsites in SharePoint Online using PnP PowerShell
    $WebsCollection = Get-PnPSubWeb -Recurse
    
    #Iterate through each subsite
    ForEach($Web in $WebsCollection)
    {
        Write-host $Web.Url
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

The -Recurse switch in the Get-PnPSubWeb cmdlet gets all subsites from all levels of the site collection recursively; if you want to include the root site of the site collection, use: the -IncludeRootWeb switch.

#Parameter
$SiteURL = "https://crescent.sharepoint.com/sites/marketing" 

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive
 
    #Get all webs in SharePoint Online site using PnP PowerShell
    $WebsCollection = Get-PnPSubWeb -Recurse -IncludeRootWeb
     
    #Iterate through each web in site
    ForEach($Web in $WebsCollection)
    {
        Write-host $Web.Url
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

To get all sites and subsites in SharePoint Online using PowerShell, use: Get All Sites and Subsites in SharePoint Online using PowerShell

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!

8 thoughts on “SharePoint Online: PowerShell to Get All Subsites in a Site Collection

Leave a Reply

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