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

%3CLINGO-SUB%20id%3D%22lingo-sub-137157%22%20slang%3D%22en-US%22%3EPowerShell%20script%20to%20get%20the%20list%20of%20all%20the%20SharePoint%20site%20collection%20administrators%20in%20the%20tenant%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-137157%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20Can%20someone%20help%20me%20with%20a%20Powershell%20script%20that%20helps%20in%20fetching%20the%20list%20of%20all%20the%20site%20collection%20administrators%20as%20well%20as%20the%20corresponding%20Site%20URL%20.I%20tried%20the%20below%20mentioned%20two%20approach%20but%20they%20didn't%20give%20the%20desired%20results%20.%3C%2FP%3E%0A%3CP%3E%3CU%3EApproach%201%20%3A%26nbsp%3B%3C%2FU%3E%3CU%3EThis%20one%20will%20give%20only%20the%20primary%20admin%20details%20for%20all%20the%20site%20collections%3C%2FU%3E%20%3A%3C%2FP%3E%0A%3CP%3E%24sites%20%3D%20Get-SPOSite%20-Limit%20All%3C%2FP%3E%0A%3CP%3E%24sites%20%7C%20select%20url%2Cowner%3C%2FP%3E%0A%3CP%3E%3CU%3EApproach%202%20%3A%26nbsp%3B%3C%2FU%3E%3CU%3EI%20wrote%20this%20one%20to%20get%20the%20list%20of%20all%20the%20admins%20for%20the%20all%20the%20site%20collections%20and%20it%20doesn%E2%80%99t%20give%20me%20the%20expected%20results%20.%26nbsp%3B%3C%2FU%3E%3C%2FP%3E%0A%3CP%3E%24Creds%20%3D%20Get-Credential%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%24site%20%3D%20%E2%80%98%3CA%20href%3D%22https%3A%2F%2Ftenant-admin.sharepoint.com%E2%80%99%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Ftenant-admin.sharepoint.com%E2%80%99%3C%2FA%3E%3C%2FP%3E%0A%3CP%3EConnect-SPOService%20-Url%20%24site%20-Credential%20%24Creds%3C%2FP%3E%0A%3CP%3E%24AllSite%3D%20Get-SPOSite%20-Limit%20All%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%24AllUsers%20%3D%20Get-SPOUser%20-Site%20%24AllSite%20-Limit%20all%20%7C%20select%20DisplayName%2C%20LoginName%2CIsSiteAdmin%3C%2FP%3E%0A%3CP%3E%24AllUsers%20%7C%20Export-Csv%20-Path%20C%3A%5CUsers%5CDesktop%5Callusers.csv%20-NoTypeInformation%20-Force%3C%2FP%3E%0A%3CP%3E%24Data%20%3D%20Import-Csv%20C%3A%5CUsers%5CDesktop%5Callusers.csv%3C%2FP%3E%0A%3CP%3Eforeach(%24aUser%20in%20%24Data)%3C%2FP%3E%0A%3CP%3E%7B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20if(%24aUser.IsSiteAdmin%20-eq%20%E2%80%9CTrue%E2%80%9D)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%7B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Write-Host%20%24aUser.DisplayName%20%24aUser.LoginName%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%7D%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-137157%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESharePoint%20Online%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-137238%22%20slang%3D%22en-US%22%3ERe%3A%20PowerShell%20script%20to%20get%20the%20list%20of%20all%20the%20SharePoint%20site%20collection%20administrators%20in%20the%20te%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-137238%22%20slang%3D%22en-US%22%3E%3CP%3Eyou%20can%20try%20this%20modified%20script%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%24Creds%20%3D%20Get-Credential%0A%0A%20%20%24site%20%3D%20%E2%80%98https%3A%2F%2Ftenant-admin.sharepoint.com%E2%80%99%0A%0AConnect-SPOService%20-Url%20%24site%20-Credential%20%24Creds%0A%0A%24AllSites%3D%20Get-SPOSite%20-Limit%20All%0A%0A%24users%20%3D%20%40()%3B%0Aforeach%20(%24Allsite%20in%20%24AllSites)%0A%7B%0A%0A%24AllUsers%20%3D%20Get-SPOUser%20-Site%20%24AllSite.Url%20-Limit%20all%20%7C%20select%20DisplayName%2C%20LoginName%2CIsSiteAdmin%0A%24users%2B%3D%24AllUsers%0A%24AllUsers%20%3D%20%24null%0A%23Write-Host%20%24AllSite.Url%22%20completed%22%0A%0A%7D%0A%24users%20%7C%20Export-Csv%20-Path%20%22C%3A%5CUsers%5CDesktop%5Callusers.csv%22%20-NoTypeInformation%20-Force%0A%0A%24Data%20%3D%20Import-Csv%20%22C%3A%5CUsers%5CDesktop%5Callusers.csv%22%0A%0Aforeach(%24aUser%20in%20%24Data)%0A%0A%7B%0A%0A%20%20if(%24aUser.IsSiteAdmin%20-eq%20%E2%80%9CTrue%E2%80%9D)%0A%0A%20%20%7B%0A%0A%20%20%20%20Write-Host%20%24aUser.DisplayName%20%24aUser.LoginName%0A%0A%20%20%7D%0A%20%20%7D%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-137175%22%20slang%3D%22en-US%22%3ERe%3A%20PowerShell%20script%20to%20get%20the%20list%20of%20all%20the%20SharePoint%20site%20collection%20administrators%20in%20the%20te%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-137175%22%20slang%3D%22en-US%22%3E%3CP%3EHello%2C%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EMaybe%20this%20help%20you%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EGet%20all%20site%20collection%3C%2FSTRONG%3E%3CSTRONG%3Es%20and%20their%20subsites%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fna01.safelinks.protection.outlook.com%2F%3Furl%3Dhttps%253A%252F%252Fgallery.technet.microsoft.com%252Foffice%252FGet-all-collections-and-be30148a%26amp%3Bdata%3D02%257C01%257Cpmoreno%2540microsoft.com%257Ca8367f6b483f402215df08d537d1e2c9%257C72f988bf86f141af91ab2d7cd011db47%257C1%257C0%257C636476296963969231%26amp%3Bsdata%3DEH8ZpVLE84RTbdj1uZuXfd3%252BFViuQ4lLODYyFMtK57k%253D%26amp%3Breserved%3D0%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgallery.technet.microsoft.com%2Foffice%2FGet-all-collections-and-be30148a%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ERegards%3C%2FP%3E%3C%2FLINGO-BODY%3E
Frequent Contributor

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

  }
  }