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

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, and this can be done using PowerShell. In this blog post, we 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="[email protected]"
$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 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

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 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: -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 is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

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

Leave a Reply