SharePoint Online: PowerShell to Iterate through All Site Collections
Requirement: PowerShell to iterate through all site collections in SharePoint Online Tenant.
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 the 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 script first 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.
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
}
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 the theme or navigation of a site, or the Add-PnPUserToGroup cmdlet to add users to a group.
Iterate through All Sites – Excluding Certain Site Types
How about excluding Seach Center, Mysite Host, Redirect sites, 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, 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, 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
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.
Hello,
I need a PowerShell script to get the details of the members inside all the sites
PowerShell for SharePoint online
This should help: Site Users and Groups Report for SharePoint Online using PowerShell