SharePoint Online: Loop Through All Subsites in a Site Collection using PowerShell

Requirement: We often got to loop through each subsite under a site collection while working with SharePoint Online using PowerShell.

PowerShell CSOM Script to Iterate through All Subsites in a Site Collection in SharePoint Online:

Looping through all subsites in a SharePoint Online site collection using PowerShell is essential for managing and automating your SharePoint Online environment. Looping through all subsites allows you to retrieve information about subsites and perform various actions, such as creating or deleting artifacts. This article will discuss using PowerShell to loop through all subsites in a SharePoint Online site collection.

This script iterates through each subsite and lists all subsite URLs:

#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 for Processing
$SiteCollUrl = "https://crescent.sharepoint.com/sites/projects"
 
#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
#Custom function to get all subsites in a given site collection
Function Get-SPOWebs($Url, $Credentials)
{
    #Set up the context
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($Url) 
    $Context.Credentials = $credentials

    $web = $Context.Web
    $Context.Load($web)
    $Context.Load($web.Webs)
    $Context.ExecuteQuery()

    #Do Something Here
    Write-host $Web.URL

    #Process Each subsite in current site
    ForEach($web in $web.Webs)
    {
        #call the function recursively
        Get-SPOWebs $web.Url $Credentials
    }
} 

#call the function                   
Get-SPOWebs $SiteCollUrl $Credentials

Here is a practical example of looping through all sub-site in a site collection and changing the Logo on each subsite: How to Change Logo in SharePoint Online using PowerShell?

How to iterate through all subsites in SharePoint Online using PnP PowerShell?

The PnP PowerShell version of looping through subsites goes like this with Get-PnPSubWeb cmdlet:

#Parameter
$SiteURL = "https://crescent.sharepoint.com/sites/projects"
 
Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive
  
    #Get all subsites in a SharePoint Online site
    $Subsites = Get-PnPSubWeb -Recurse -IncludeRootWeb
      
    #Iterate through each subsite
    ForEach($site in $Subsites)
    {
        Write-host $Site.Url
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

3 thoughts on “SharePoint Online: Loop Through All Subsites in a Site Collection using PowerShell

  • I am trying to nest inside foreach site in subsite a foreach list in subsite, but it keeps displaying the root web lists for each subsite, not the lists in each web/subsite… how do I load the lists for the current foreach $site

    Reply
  • Could you please let me know how to output this to CSV in the format SiteCollURL SubsiteURL (level does not matter)?

    Reply

Leave a Reply

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