SharePoint Online: PowerShell to Iterate through All Site Collections

sharepoint online powershell iterate through all site collections

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

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 enumerate all the sites so that you can do some further processing on them. Whatever the reason, PowerShell can help! Whether it’s auditing, reporting, or applying changes, manually accessing each site collection can be time-consuming and inefficient. This is where PowerShell comes to the rescue! In this article, let’s see how to use PowerShell to cycle through all the sites in your SharePoint Online tenant.

How to loop through all SharePoint sites?

There are different ways to do this. Let’s use the SharePoint Online PowerShell cmdlet Get-SPOSite to retrieve all sites in the tenant and then iterate through them. The Get-SPOSite cmdlet is the primary command for retrieving site collections in SharePoint Online. It allows you to retrieve all site collections or filters based on specific criteria.

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

First, this script connects to SharePoint Online using the Connect-SPOService cmdlet and prompts you to enter your login credentials. Then, it gets all site collections in the tenant using the Get-SPOSite cmdlet. Finally, it iterates through each site collection and performs an operation (in this case, getting the site URL). This can be useful for tasks such as exporting site data or simply getting a list of all sites on the farm.

Within the loop, you can access properties and perform actions on each site collection. Some common actions include:

  • Updating site collection properties (e.g., title, description)
  • Modifying site collection permissions or owners
  • Enabling or disabling site collection features
  • Generating reports or collecting data from site collections

Loop through all sites in SharePoint Online using PowerShell

Let’s see an example of generating reports or collecting data from all site collections from the tenant:

#Connect to Admin Center
Connect-SPOService -Url "https://crescent-admin.sharepoint.com"

#Get All site collections
$SiteCollections = Get-SPOSite -Limit All
$SiteReport = @()

#Iterate through each site
ForEach ($site in $siteCollections) {
    # Generate a report of site collection properties
    $siteReport += [PSCustomObject]@{
        URL = $Site.Url
        StorageUsage = $Site.StorageUsageCurrent
    }
}
$SiteReport
$SiteReport | Export-Csv -Path "C:\TempSiteCollectionReport.csv" -NoTypeInformation

Similarly, to perform some updates to all sites, Here is how the script goes:

#Get all sites
$SiteCollections = Get-SPOSite -Limit All

#Iterate through sites and update site title
ForEach ($site in $siteCollections) {
    # Update site collection title
    $NewTitle = "Crescent Inc $($site.Url)"
    Set-SPOSite -Identity $Site.Url -Title $NewTitle
}

Handling Large Number of Site Collections in the Loop

When dealing with a large number of site collections, it’s important to consider performance and throttling. SharePoint Online has throttling limits in place to prevent excessive resource consumption. To handle large datasets, you can use techniques like batching, pagination, or leveraging the -Limit parameter of the Get-SPOSite cmdlet.

Here is an example of SharePoint Online PowerShell loop through all site collections with batching:

#Connect to Admin Center
Connect-SPOService -Url "https://crescent-admin.sharepoint.com"

$batchSize = 10
$siteCollections = Get-SPOSite -Limit All

for ($i = 0; $i -lt $siteCollections.Count; $i += $batchSize) {
    $batch = $siteCollections[$i..($i + $batchSize - 1)]
    foreach ($site in $batch) {
        # Perform actions on each site collection in the batch
        Write-Host "Processing site collection: $($site.Url)"
        # Add your custom code here
    }
    Start-Sleep -Seconds 5 # Pause to avoid throttling
}

Error Handling and Logging Inside Loop

When iterating through site collections, it’s crucial to implement proper error handling and logging. Use try-catch blocks to handle potential errors and exceptions gracefully. Log any errors or warnings to a file or centralized logging system for later analysis and troubleshooting.

$LogFile = "SiteCollectionProcessing.log"
$SiteCollections = Get-SPOSite -Limit All

foreach ($site in $siteCollections) {
    try {
        # Perform actions on each site collection
        Write-Host "Processing site collection: $($site.Url)"
        # Add your custom code here
    }
    catch {
        $errorMessage = "Error processing site collection $($site.Url): $($_.Exception.Message)"
        Write-Warning $errorMessage
        Add-Content -Path $logFile -Value $errorMessage
    }
}

SharePoint Online: PnP PowerShell to loop through sites

If you have the PnP PowerShell module installed, you can also use the Get-PnPTenantSite cmdlet to retrieve site collections. This cmdlet provides additional filtering and properties compared to Get-SPOSite.

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
}

You can modify the script to perform any operation you need on each site collection. For example, you can use the Set-PnPWeb cmdlet to update a site’s theme or navigation or the Add-PnPGroupMember cmdlet to add users to a group.

Iterate through All Sites – Excluding Certain Site Types

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

As a SharePoint administrator, you may often find yourself in situations where you need to perform actions across multiple site collections in SharePoint Online. Let’s say you want to review the total number of documents in SharePoint. You can use PowerShell to iterate through all site collections and get the number of items in each document library.

#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, Redirect sites, Mysite Host, App Catalog, Content Type Hub, eDiscovery and Bot Sites
$SiteCollections = Get-PnPTenantSite | Where { $_.URL -like '*/sites*' -and $_.Template -NotIn ("SRCHCEN#0", "REDIRECTSITE#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
        Connect-PnPOnline -Url $Site.URL -Credentials $Cred

        #Get all document libraries from the site
        $DocumentLibraries = Get-PnPList | 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
        }
    }
    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 that 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
}

Conclusion

In conclusion, using PowerShell is a convenient way to iterate through all site collections in SharePoint Online and perform operations on them. By using the script provided in this tutorial, you can easily connect to SharePoint Online, get a list of all site collections in the tenant, and iterate through each site collection to perform a specific operation. This can be useful when you need to perform a batch operation on multiple site collections, or when you want to automate the process of managing your SharePoint Online environment.

If you want to loop through all site collections and subsites, refer to: SharePoint Online: PowerShell to Get All Site Collections and Subsites

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

2 thoughts on “SharePoint Online: PowerShell to Iterate through All Site Collections

Leave a Reply

Your email address will not be published. Required fields are marked *