Migration report with CLI for Microsoft 365

Sometimes you need a quick report on your Microsoft 365 tenant. Recently I was asked to quickly report the number of sites to give a ‘guestimate’ on a tenant-to-tenant migration. Something the CLI for Microsoft 365 should be able to do. We decided to deliver an overview of all sites and their lists, but no other info was needed for this first report.
CLI for Microsoft 365 magic
There are a few great samples on the CLI for Microsoft 365 docs, but I needed some other info. I decided to use a sample by Patrick to list all site collection owners and tweak that for my needs. Getting the sites is easy with the m365 spo site classic list
and retrieving all lists is not complex either m365 spo list list -u https://contoso.sharepoint.com
The sample output I was looking for was something like
"Type","Title","Url","StorageUsage","Template"
"site","","https://digiwijs.sharepoint.com/search","27","SRCHCEN#0"
"list","appdata","/search/_catalogs/appdata",,"125"
With that the full script looks as follows:
$fileExportPath = "<PUTYOURPATHHERE.csv>"
$m365Status = m365 status
if ($m365Status -eq "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."
I am really happy to see that it was that easy just to get the output I needed; next time I will try to get some bash
in there as well 💻.