PowerShell script to get the list of all the SharePoint site collection administrators in the tenant

MVP

Hi ,

  Can someone help me with a Powershell script that helps in fetching the list of all the site collection administrators as well as the corresponding Site URL .I tried the below mentioned two approach but they didn't give the desired results .

Approach 1 : This one will give only the primary admin details for all the site collections :

$sites = Get-SPOSite -Limit All

$sites | select url,owner

Approach 2 : I wrote this one to get the list of all the admins for the all the site collections and it doesn’t give me the expected results . 

$Creds = Get-Credential

 $site = ‘https://tenant-admin.sharepoint.com’

Connect-SPOService -Url $site -Credential $Creds

$AllSite= Get-SPOSite -Limit All

 

$AllUsers = Get-SPOUser -Site $AllSite -Limit all | select DisplayName, LoginName,IsSiteAdmin

$AllUsers | Export-Csv -Path C:\Users\Desktop\allusers.csv -NoTypeInformation -Force

$Data = Import-Csv C:\Users\Desktop\allusers.csv

foreach($aUser in $Data)

{

  if($aUser.IsSiteAdmin -eq “True”)

  {

    Write-Host $aUser.DisplayName $aUser.LoginName

  }

 

 

2 Replies

you can try this modified script

 

$Creds = Get-Credential

  $site = ‘https://tenant-admin.sharepoint.com’

Connect-SPOService -Url $site -Credential $Creds

$AllSites= Get-SPOSite -Limit All

$users = @();
foreach ($Allsite in $AllSites)
{

$AllUsers = Get-SPOUser -Site $AllSite.Url -Limit all | select DisplayName, LoginName,IsSiteAdmin
$users+=$AllUsers
$AllUsers = $null
#Write-Host $AllSite.Url" completed"

}
$users | Export-Csv -Path "C:\Users\Desktop\allusers.csv" -NoTypeInformation -Force

$Data = Import-Csv "C:\Users\Desktop\allusers.csv"

foreach($aUser in $Data)

{

  if($aUser.IsSiteAdmin -eq “True”)

  {

    Write-Host $aUser.DisplayName $aUser.LoginName

  }
  }