List site collections and their lists
Summary
This script helps you to list and export all site collection and their lists SharePoint Online sites, ideal for getting insights into the size of your environment.
$fileExportPath = "<PUTYOURPATHHERE.csv>"
$m365Status = m365 status
if ($m365Status -match "Logged Out") {
# Connection to Microsoft 365
m365 login
}
$results = @()
Write-host "Retrieving all sites..."
$allSPOSites = m365 spo site classic list -o json | ConvertFrom-Json
$siteCount = $allSPOSites.Count
Write-Host "Processing $siteCount sites..."
#Loop through each site
$siteCounter = 0
foreach ($site in $allSPOSites) {
$siteCounter++
Write-Host "Processing $($site.Url)... ($siteCounter/$siteCount)"
$results += [pscustomobject][ordered]@{
Type = "site"
Title = $site.Title
Url = $site.Url
StorageUsage = $site.StorageUsage
Template = $site.Template
}
Write-host "Retrieving all lists..."
$allLists = m365 spo list list -u $site.url -o json | ConvertFrom-Json
foreach ($list in $allLists) {
$results += [pscustomobject][ordered]@{
Type = "list"
Title = $list.Title
Url = $list.Url
Template = $list.BaseTemplate
}
}
}
Write-Host "Exporting file to $fileExportPath..."
$results | Export-Csv -Path $fileExportPath -NoTypeInformation
Write-Host "Completed."
Check out the CLI for Microsoft 365 to learn more at: https://aka.ms/cli-m365
Source Credit
Sample first appeared on List site collections and their lists | CLI for Microsoft 365
Contributors
Author(s) |
---|
Albert-Jan Schot |
Disclaimer
THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.