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
}
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
Use this as your reference: How to get all lists in a SharePoint Online site collection?
Could you please let me know how to output this to CSV in the format SiteCollURL SubsiteURL (level does not matter)?