Skip to content

Instantly share code, notes, and snippets.

@plamber
Created March 10, 2021 05:16
Show Gist options
  • Select an option

  • Save plamber/dfbb0999646921af2bf2f603b5f36f01 to your computer and use it in GitHub Desktop.

Select an option

Save plamber/dfbb0999646921af2bf2f603b5f36f01 to your computer and use it in GitHub Desktop.
List all site collection owners with PnP-PowerShell
$tenant = "<PutYourTenantIdHere>"
$env:ClientId = "<PUTYourAppIdHere>"
$env:ClientSecret = "<PUTYourAppSecretHere>"
$fileExportPath = "<PUTYOURPATHHERE.csv>"
Connect-PnPOnline https://$tenant-admin.sharepoint.com -ClientId $env:ClientId -ClientSecret $env:ClientSecret
$results = @()
$sites = Get-PnPTenantSite
$sites | ForEach-Object {
$site = $_
Write-Host "Processing $($site.Url)"
Connect-PnPOnline $site.Url -ClientId $env:ClientId -ClientSecret $env:ClientSecret
$owners = Get-PnPSiteCollectionAdmin
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."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment