SharePoint Online: PowerShell to Iterate through All Site Collections

Requirement: PowerShell to iterate through all site collections in SharePoint Online Tenant

sharepoint online powershell iterate through all site collections

How to loop through all sites in SharePoint Online?

Have you ever wanted to loop through all the sites in your SharePoint Online environment? Maybe you want to get a list of all site owners, or simply enumerate all the sites so that you can do some further processing on them. Whatever the reason, PowerShell can help! In this article, let’s see how to use PowerShell to cycle through all the sites in your SharePoint Online tenant.

Loop through all sites in SharePoint Online using PowerShell

There are different ways to do this. Let’s use SharePoint Online PowerShell cmdlet Get-SPOSite to retrieve all sites in the tenant and then iterate through them.

#Parameter
$AdminSiteURL = "https://crescent-admin.sharepoint.com"

#Connect to SharePoint Online Admin Center
Connect-SPOService -Url $AdminSiteURL
 
#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
}

This can be useful for tasks such as exporting site data or simply getting a list of all sites on the farm.

SharePoint Online: PnP PowerShell to loop through sites

This PnP PowerShell script loops through all sites in your SharePoint Online environment and gets you the site URL.

#Parameter
$AdminSiteURL = "https://crescent-admin.sharepoint.com"

Try {
    #Connect to Admin Center
    Connect-PnPOnline -Url $AdminSiteURL -Interactive
 
    #Get All Site collections  
    $SitesCollection = Get-PnPTenantSite
 
    #Loop through each site collection
    ForEach($Site in $SitesCollection)  
    {  
        Write-host -F Green $Site.Url  
    }
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

Iterate through All Sites – Excluding Certain Site Types

How about excluding Seach Center, Mysite Host, App Catalog, Content Type Hub, eDiscovery, and Bot Sites and getting all sites under “/sites/” managed path?

#Config Variables
$TenantAdminURL = "https://crescent-admin.sharepoint.com"

#Get Credentials to connect
$Cred = Get-Credential

#Connect to Admin Center using PnP Online
Connect-PnPOnline -Url $TenantAdminURL -Credentials $Cred

#Get All Site collections - Exclude: Seach Center, Mysite Host, App Catalog, Content Type Hub, eDiscovery and Bot Sites
$SiteCollections = Get-PnPTenantSite | Where { $_.URL -like '*/sites*' -and $_.Template -NotIn ("SRCHCEN#0", "SPSMSITEHOST#0", "APPCATALOG#0", "POINTPUBLISHINGHUB#0", "EDISC#0", "STS#-1")}

#Loop through each site collection
ForEach($Site in $SiteCollections)
{
    Try {
        Write-host "Processing Site:"$Site.URL -f Yellow
        #Connect to the site
        $SiteConn = Connect-PnPOnline -Url $Site.URL -Credentials $Cred -ReturnConnection

        #Get all document libraries from the site
        $DocumentLibraries = Get-PnPList -Connection $SiteConn | Where-Object {$_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $False -and $_.ItemCount -gt 0}

        #Iterate through document libraries
        ForEach ($List in $DocumentLibraries)
        {
            Write-host "`tTotal Number of Items in '$($List.Title)':" $List.itemCount -f Green
        }
        Disconnect-PnPOnline -Connection $Siteconn
    }
    Catch {
        write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
    }
}

This can be handy if you need to get a list of all the sites.

PowerShell CSOM script to iterate through all site collections

Please note, the CSOM method gets only classic sites (No modern sites or communication sites are included in this approach!)

#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"
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.Online.SharePoint.Client.Tenant.dll"
 
#Set Parameters
$AdminCenterURL = "https://crescent-admin.sharepoint.com/"

Try {
    #Setup Credentials to connect
    $Cred= Get-Credential
     
    #Setup the Context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($AdminCenterURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
    #Get the tenant object 
    $Tenant = New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($Ctx)
 
    #Get all site collections
    $Sites= $Tenant.GetSiteProperties(0, $true)
    $Ctx.Load($Sites)
    $Ctx.ExecuteQuery()
     
    ForEach($Site in $Sites)
    {
        Write-Host $Site.URL
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

Related post: SharePoint Online: PowerShell to Get All Site Collections and Subsites

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!

Leave a Reply