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
How to get all subsites in SharePoint Online using PowerShell? Here is the CSOM PowerShell to list all sites and subsites in SharePoint Online:
SharePoint Online: PowerShell to List All Subsites in All Site Collections
Lets get subsites from all site collections from the tenant. Here is the PowerShell script to get all site collections and subsites in SharePoint Online:
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 PowerShell. This script retrieves all site collections and their subsites recursively 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.
Related Posts:
SharePoint Online PowerShell to list all site collections and subsites
How to get all subsites in SharePoint Online using PowerShell? 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 the another script:
SharePoint Online: PowerShell to List All Subsites in All Site Collections
Lets 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://crescenttech-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) }
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 PowerShell. 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 Site Collection administrators of the site $SubSites = Get-PnPSubWebs -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
Make sure you have Site collection Administrator rights on all site collections in the tenant before executing this script. Otherwise, you may get "The remote server returned an error: (401) Unauthorized." error! PowerShell to Add Site Collection Administrator to All Sites in SharePoint Online
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, Mysite Host, App Catalog, Content Type Hub, eDiscovery and Bot Sites $SitesCollections = Get-PnPTenantSite | Where -Property Template -NotIn ("SRCHCEN#0", "SPSMSITEHOST#0", "APPCATALOG#0", "POINTPUBLISHINGHUB#0", "EDISC#0", "STS#-1") #Loop through each site collection $SubSitesData = @() ForEach($Site in $SitesCollections) { #Connect to site collection Write-host -f Yellow "Processing site:"$Site.Url $SiteConn = Connect-PnPOnline -Url $Site.Url -Credentials $Cred #Get the SubSites of the site collection $Webs = Get-PnPSubWebs -Recurse -Connection $SiteConn #Collect Subsites count $SubSitesData += New-Object PSObject -Property ([ordered]@{ SiteName = $Site.Title URL = $Site.URL SubSitesCount = $Webs.Length }) Disconnect-PnPOnline -Connection $SiteConn } $SubSitesData
Related Posts:
No comments:
Please Login and comment to get your questions answered!