GitHub Icon Image

List site collection owners

Summary

This script helps you to list and export all site collection owners in your SharePoint Online sites.

  • CLI for Microsoft 365 with PowerShell
$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
$counter = 0
foreach($site in $allSPOSites){
    $counter++
    Write-Host "Processing $($site.Url)... ($counter/$siteCount)"
    $users = m365 spo user list --webUrl $site.Url -o json | ConvertFrom-Json
    $owners = $users.value | where { $_.IsSiteAdmin -eq $true } 
    
    foreach($owner in $owners){
        $results += [pscustomobject][ordered]@{
            SiteUrl = $site.Url
            LoginName = $owner.LoginName
            Title = $owner.Title
            Email = $owner.Email
        }
    }
}
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 collection owners | CLI for Microsoft 365

Contributors

Author(s)
Patrick Lamber

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.

Back to top Script Samples
Generated by DocFX with Material UI