SharePoint Online sites can have thousands of users. There are times when people are asked to get site collection admins using SharePoint Online Management Shell or Get-SPOUser cmdlet with Where $_.IsSiteAdmin. This will result in timeout if there are lot of users on the site. Instead I decided to use a different approach. First get all the users from site into a CSV and then make the check.
$Creds = Get-Credentials
$site = ‘https://sharepoint-admin.sharepoint.com’
Connect-SPOService -Url $site -Credential $Creds
$AllUsers = Get-SPOUser -Site https://site.sharepoint.com -Limit all | select DisplayName, LoginName,IsSiteAdmin
$AllUsers | Export-Csv -Path C:\temp\allusers.csv -NoTypeInformation -Force
$Data = Import-Csv C:\temp\allusers.csv
foreach($aUser in $Data)
{
if($aUser.IsSiteAdmin -eq “True”)
{
Write-Host $aUser.DisplayName $aUser.LoginName
}
}