kwizcom banner advertisement

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 Script to get all Site Collections in SharePoint Online:
Lets use PowerShell to get a list of Site collections from SharePoint online.
#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

#Get All Site Collections
$SiteColl = Get-SPOSite 

#Iterate through each site collection
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 

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

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

Get All site collections that are using > 1GB storage
Get-SPOSite -Limit All | where { $_.StorageUsageCurrent -gt 1024 } 

Get Site Collections List in a HTML Report format
Lets 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.