SharePoint Online: Get All Site Collections using PowerShell

Requirement: Get all site collections using PowerShell in SharePoint Online.

How to Get a List of Site Collections in SharePoint Online? 
If you want to get a list of SharePoint online site collections, you can go to SharePoint admin center (Typically at: https://YOURCOMPANY-admin.sharepoint.com/_layouts/15/online/SiteCollections.aspx).
Get All Site Collections in SharePoint online using PowerShell

This gets you the list of all site collections in SharePoint Online. Lets see the SharePoint online PowerShell to get all site collections.

PowerShell to Get All Site Collections in SharePoint Online:
Site collections are the primary objects in SharePoint Online. Use the Get-SPOSite PowerShell cmdlet to get a list of all site collections in the tenant. Here is the PowerShell to list all SharePoint Online sites.
#Load SharePoint Online 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"
 
#Variables for Processing
$AdminUrl = "https://crescent-admin.sharepoint.com/"
$UserName= "Salaudeen@crescent.com"
$Password = "Password goes here"
 
#Setup Credentials to connect
$SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
$Credentials = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $UserName, $SecurePassword

#connect to the service
Connect-SPOService -Url $AdminUrl -Credential $Credentials

#sharepoint online get all site collections powershell
$SiteColl = Get-SPOSite 

#sharepoint online powershell iterate through all site collections
foreach($Site in $SiteColl)
{
    Write-host $Site.Url
}
Similar to On-Premises SharePoint Management Shell, You can also use "SharePoint Online Management Shell" to connect to Office 365, SharePoint online sites!
Import-Module Microsoft.Online.Sharepoint.PowerShell -DisableNameChecking

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

$Credential = Get-credential 
Connect-SPOService -url $AdminSiteURL -Credential $Credential 

#sharepoint online list all site collections powershell
Get-SPOSite -Detailed | Format-Table Url, Template, StorageUsageCurrent, StorageQuota, LastContentModifiedDate -AutoSize 

Export All Site Collection inventory to CSV:
$Credential = Get-credential
Connect-SPOService -url "https://crescent-admin.sharepoint.com" -Credential $Credential
Get-SPOSite -Detailed | Export-CSV -LiteralPath C:\SitesInventory.csv -NoTypeInformation
This PowerShell lists all site collections in SharePoint Online

PowerShell to Get All Site Collections including Modern Team sites and Group Sites:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\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"
  
#Get All Site collections from the Tenant- Including Modern Team sites and communication sites
Function Get-SPOSites($AdminSiteURL, $Cred)
{
    #Setup credentials to connect
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
   
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($AdminSiteURL)
    $Ctx.Credentials = $Credentials
  
    #Get the tenant object 
    $Tenant = New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($ctx)
  
    #Get All Site Collections
    $SiteCollections=$Tenant.GetSitePropertiesFromSharePoint(0,$true)
    $Ctx.Load($SiteCollections)
    $Ctx.ExecuteQuery()
 
    #Iterate through Each site collection
    ForEach($Site in $SiteCollections)
    {
        Write-host $Site.URL
    }
}
  
#Set Parameters
$AdminSiteUrl = "https://crescent-admin.sharepoint.com/"
$Cred= Get-Credential

#sharepoint online powershell list all sites
Get-SPOSites -AdminSiteURL $AdminSiteUrl -Cred $Cred

SharePoint Online PowerShell to List All Site Collections
Lets get all site collections under a specific path:
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

#Connect to SharePoint Online Admin Center
Connect-SPOService -Url $AdminSiteURL –Credential $Cred

#Get All site collections which has /sites in its path
Get-SPOSite -Filter { Url -like '*/sites*' } -Limit All

#Get All site collections of Group site template
#Get-SPOSite -Filter { Template -eq "GROUP#0" } -Limit All
You can filter and get all site collections based on some criteria. E.g. Let's get all sites that are using > 1GB storage
Get-SPOSite -Limit All | where { $_.StorageUsageCurrent -gt 1024 } 

SharePoint Online PnP PowerShell to Get All Sites
#Set Parameter
$TenantSiteURL="https://crescent.sharepoint.com"

#Connect to the Tenant site
Connect-PnPOnline $TenantSiteURL -Credentials (Get-Credential)

#Get all site collections
Get-PnPTenantSite
This gets all site collections including modern team sites and communication sites. You can include OneDrive sites as:
#Get all site collections including OneDrive Sites
Get-PnPTenantSite -IncludeOneDriveSites

Get Site Collections List in a HTML Report format
Let's get a list of all site collections in a nice HTML format using PowerShell:
Import-Module Microsoft.Online.SharePoint.Powershell -DisableNameChecking

#Config Parameters
$AdminSiteURL="https://crescent-admin.sharepoint.com"
$ReportOutput="C:\Temp\SitesRpt.html"

#CSS Styles
$HeadTag = @"
<style type="text/css">

table {
 border-collapse: collapse; font-family: verdana,arial,sans-serif;
 font-size:11px; color:#333333; border-width: 1px; border-color: #a9c6c9;
 border: b1a0c7 0.5pt solid; border-spacing: 1px; border-collapse: separate; /*Sal Table format */  
}

th {
border-width: 1px; padding: 5px; background-color:#8064a2;
border: #b1a0c7 0.5pt solid; font-family: Calibri; height: 15pt; 
color: white;  font-size: 11pt;  font-weight: 700;  text-decoration: none;
}

td {
 border: #b1a0c7 0.5pt solid; font-family: Calibri; height: 15pt; color: black; 
 font-size: 11pt; font-weight: 400; text-decoration: none; 
}

tr:nth-child(even) { background-color: #e4dfec; }
tr:hover { background-color: #694D8C; color:#ffffff }

</style>
"@

$PreContentTag = "<h3> SharePoint Online: Site Collections Inventory Report </h3>"

#Get Credentials to connect to SharePoint Admin Center
$Cred = Get-Credential

#Connect to SharePoint Online Admin Center
Connect-SPOService -Url $AdminSiteURL –Credential $Cred

#Get All site collections
$SiteCollections = Get-SPOSite -Limit All
Write-Host "Total Number of Site collections Found:"$SiteCollections.count -f Yellow

#Get the site collection details, convert to html and output to the report
$SiteCollections | ConvertTo-HTML -Title "Site Collection Inventory Report" -Property URL, Title,@{Expression={$_.LastContentModifiedDate }; Label="Last Modified"} -Head $HeadTag -PreContent $PreContentTag | Out-File $ReportOutput
and the report looks like:
sharepoint online site collections html report

Here is my another article on how to use PowerShell with SharePoint Online: How to Use PowerShell with SharePoint Online?
SharePoint Online: Get All Site Collections using PowerShell SharePoint Online: Get All Site Collections using PowerShell Reviewed by Salaudeen Rajack on February 27, 2016 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.