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 just 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
}

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