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
$AdminSiteURL="https://crescent-admin.sharepoint.com"
#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
$Context.Load($web)
$Context.Load($web.Webs)
$Context.executeQuery()
#Iterate through each subsite in the current web
foreach ($Subweb in $web.Webs)
{
#Get the web object
$Subweb
#Call the function recursively to process all subsites underneath the current web
Get-SPOWeb($Subweb.url)
}
}
#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
$Ctx.Load($Web)
$Ctx.Load($Web.Webs)
$Ctx.ExecuteQuery()
#Do something
Write-host $Web.Url
#Loop through each each subsite in site
Foreach($Web in $web.Webs)
{
Get-SPOWebs $Web.Url
}
}
#Variables
$AdminCenterURL = "https://Crescent-admin.sharepoint.com"
#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
Get-SPOWebs($Site.URL)
}
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 the PnP PowerShell module. This script retrieves all site collections and their subsites recursively using PnP PowerShell.
#Set 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 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 Sub Sites Of the site collections
$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.
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.
#Parameters
$TenantAdminURL = "https://Crescent-Admin.SharePoint.com"
#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 SharePoint 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
})
}
$SubSitesData
Export a List of All Sites and Subsites to a CSV
How do I get all SharePoint sites and subsites in PowerShell? Here is the PnP PowerShell to get all site collections and subsites in your tenant and export them to a CSV file.
#Set Variables
$TenantURL = "https://crescent.sharepoint.com"
$ExportFile = "C:\Temp\SitesandSubsites.csv"
$AllSitesList = @()
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $TenantURL -Interactive
#Get All Site collections - Exclude certain site templates such as personal sites
$SiteCollections = Get-PnPTenantSite | Where -Property Template -NotIn ("SRCHCEN#0","REDIRECTSITE#0", "SPSMSITEHOST#0", "APPCATALOG#0", "POINTPUBLISHINGHUB#0", "EDISC#0", "STS#-1")
Write-host "Total Number of site collections:"$SiteCollections.count
#Loop through each site collection
ForEach($Site in $SitesCollections)
{
#Connect to site collection
Connect-PnPOnline -Url $Site.Url -Interactive
#Get all Top-level site and Sub Sitess Of the SharePoint Online site collection
Get-PnPSubWeb -Recurse -IncludeRootWeb -Includes ParentWeb | ForEach-Object {
$AllSitesList += New-Object PSObject -property $([ordered]@{
SiteName = $_.Title
SiteURL = $_.Url
IsSubSite = If($_.ParentWeb.Title -eq $null) {$False} Else {$True}
})
}
}
}
Catch {
Write-host -f Red "Error:" $_.Exception.Message
}
#get all sites and subsites in sharepoint online
$AllSitesList
#Export All sites and Subsites list to a CSV list
$AllSitesList | Export-CSV -Path $ExportFile -NoTypeInformation -Encoding UTF8
Related Posts:
https://www.sharepointdiary.com/2017/05/get-all-site-collections-and-subsites-in-sharepoint-online-powershell.html
In the last script to generate CSV file there is a error in line 15:
ForEach($Site in $SitesCollection). Should be ForEach($Site in $SiteCollections).
Yes! Fixed it. Thanks.