SharePoint Online: PowerShell to Get Site Collection Administrators

Requirement:  SharePoint Online PowerShell to List All Site Collection Administrators.

SharePoint Online site collection administrators possess full control to the entire site collection - including top-level site, subsites, all lists and libraries in the site collection. Each SharePoint site collection can have its own Site Collection Administrators, Typically assigned during the time of site collection creation by SharePoint Online administrators through SharePoint Online Admin Center site, as Primary Site Owner and site collection Administrators. Any additional site collection administrators can be added/removed through site settings page.
sharepoint online get site collection administrators

Site collection administrators have same access rights as same as primary site owners but the only difference is: E-mail notifications! Primary site collection administrators are those who'll be getting Email notifications such as site collection storage limit warning Emails.

How to Get site collection administrators in SharePoint Online?
If you want to get all site collection administrators,
  • Click on Site Settings Gear, select Site Settings
  • In Site Settings page, click on "Site Collection Administrators" link under "Users and Permission" group.
  • This page gives you the list of site collection administrators on the particular site collection.
    sharepoint online get site collection admin powershell
We don't have Secondary Site Collection Administrators in SharePoint Online. A Site Collection can have one Primary Site owner and additional site collection Administrators!

Get Primary Site Collection Administrator - Owner using PowerShell:
Here is how to get site collection administrators in SharePoint Online
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.Online.SharePoint.Client.Tenant.dll"
  
#Get Primary Owners of All Site collections from the Tenant
Function Get-SPOPrimarySiteOwners($AdminSiteURL, $Cred)
{
    #Setup credentials to connect
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
   
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($AdminSiteURL)
    $Ctx.Credentials = $Credentials
  
    #Get the tenant object 
    $Tenant = New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($ctx)
  
    #Get All Site Collections
    $SiteCollections=$Tenant.GetSitePropertiesFromSharePoint(0,$true)
    $Ctx.Load($SiteCollections)
    $Ctx.ExecuteQuery()
 
    #Iterate through Each site collection
    ForEach($Site in $SiteCollections)
    {
        #Get the Site Collection and Audit Objects
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($Site.URL)
        $Ctx.Credentials = $Credentials
        
        #Get the Site collection
        $SiteCollection = $Ctx.Site
        $Ctx.Load($SiteCollection)
        $Ctx.ExecuteQuery()
 
        #Get Site Owner
        $Owner = $SiteCollection.Owner
        $Ctx.Load($Owner)
        $Ctx.ExecuteQuery()
        
        #Output Primary Site owner Details
        $SiteCollection | Select URL, @{Name="Owner Name";Expression={$_.Owner.Title}}, @{Name="Owner Email";Expression={$_.Owner.Email}}
    }
}
  
#Set Parameters
$AdminSiteUrl = "https://crescenttech-admin.sharepoint.com/"
$Cred= Get-Credential
 
Get-SPOPrimarySiteOwners -AdminSiteURL $AdminSiteUrl -Cred $Cred
This gets primary site collection administrator using PowerShell in SharePoint Online

SharePoint Online: PowerShell to Get Site Collection Administrators
You can use the PowerShell cmdlet Get-SPOUser along with the filter IsSiteAdmin property to get the list of Site collection administrators of the given site collection. This gets you both primary site owners and site collection administrators.
#Variables for processing
$AdminURL = "https://Crescent-admin.sharepoint.com/"
$AdminName = "SPAdmin@Crescent.com"
$SiteCollURL="https://Crescent.sharepoint.com/sites/sales"
 
#User Names Password to connect 
$Password = Read-host -assecurestring "Enter Password for $AdminName" 
$Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminName, $Password

#Connect to SharePoint Online
Connect-SPOService -url $AdminURL -credential $Credential

#Get the Site colection
$SiteColl = Get-SPOSite $SiteCollURL
    
#Get all Site Collection Administrators
$SiteAdmins = Get-SPOUser -Site $SiteCollURL -Limit ALL | Where { $_.IsSiteAdmin -eq $True}
foreach($Admin in $SiteAdmins)
{
    Write-host $Admin.LoginName        
}

PowerShell to Get Site Collection Administrators of All Site Collections in SharePoint Online:
Lets add some error handling and get the list of site collection administrators from all site collections.
#Variables for processing
$AdminURL = "https://Crescent-admin.sharepoint.com/"
$AdminName = "spadmin@Crescent.com"
 
#User Names Password to connect 
$Password = Read-host -assecurestring "Enter Password for $AdminName" 
$Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminName, $Password

Try {
    #Connect to SharePoint Online
    Connect-SPOService -url $AdminURL -credential $Credential

    #Get all Site colections
    $Sites = Get-SPOSite -Limit ALL

    Foreach ($Site in $Sites)
    {
        Write-host $Site.URL
    
        #Get all Site Collection Administrators
        $SiteAdmins = Get-SPOUser -Site $Site.Url -Limit ALL | Where { $_.IsSiteAdmin -eq $True}
        foreach($Admin in $SiteAdmins)
        {
            Write-host $Admin.LoginName        
        }
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
If you need to export all site collection administrators list to a CSV file, refer: Export SharePoint Online Site Collection Administrators to CSV

Get All Site Collection Administrators using PnP PowerShell in SharePoint Online
#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"

#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get Site Collection Administrators
Get-PnPSiteCollectionAdmin
This PnP PowerShell script gets all site collection administrators of a given site collection. To get site collection administrators of all sites in the tenant, use:
#Set Variables
$SiteURL = "https://crescent.sharepoint.com/"

#Get Credentials to connect
$Cred = Get-Credential

#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -Credentials $Cred

#Get All Site collections  
$SitesCollection = Get-PnPTenantSite

#Loop through each site collection
ForEach($Site in $SitesCollection)  
{  
    Write-host -F Green "Site Collection Administrators of site: " $Site.Url  
    Connect-PnPOnline -Url $Site.Url -Credentials $Cred
  
    #Get Site Collection administrators of the site
    Get-PnPSiteCollectionAdmin
}
SharePoint Online: PowerShell to Get Site Collection Administrators SharePoint Online: PowerShell to Get Site Collection Administrators Reviewed by Salaudeen Rajack on June 06, 2016 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.