Microsoft 365: Get Site Collection Administrators in SharePoint Online and Export to CSV using PowerShell

.

Hello Everyone,

Hope you all are doing well.

In this article we are going to discuss how to get Site Collection administrators in SharePoint Online and export to CSV through PowerShell.

We have good list of articles on SharePoint Online using PowerShell, please have a look.

https://knowledge-junction.in/category/technology-articles/powershell-cmdlets/sharepoint-online-powershell-cmdlets/

So without getting late, let’s get started.

Background

In our organization, one of our project requirement is get all the site collections administrators from all sites in the Tenant. To do manually its a time taking process, so we are using PowerShell script to get the site collection administrators and export the data to csv file.

Details

  • We need SharePoint Admin Site URL and Credentials.
  • Open PowerShell ISE.
  • Now ran the PowerShell with the following cmdlet to get Site Collection Data.

Note: The line starting with “#” are just description heading or comment.

Prerequisites

  • Install PowerShell ISE / PowerShell / Visual Studio Code if its not installed.
  • Install PnP Power Shell if its not installed.

#Install PnP PowerShell if its not installed
Install-Module PnP.PowerShell

Detailed Steps – PowerShell script

  • Import PnP PowerShell Module

#Import Module
Import-Module PnP.PowerShell

  • Set variables for URL, Credentials and CSV file.

#Set Variables
$AdminSiteURL = "https://osinfotech-admin.sharepoint.com/"
$credentials=Get-Credential
$ReportOutput = "C:\SiteAdmins.csv"

  • Use try to monitor the errors

#Monitor the errors
Try {}

  • Initialize a variable for the data getting from sites.

#Initialize variable for the data
$SiteAdminData = @()

  • Connect PnP Online with the Admin URL

#Connect PnPOnline with AdminSiteUrl
Connect-PnPOnline -Url $AdminSiteURL -Credentials $credentials

  • Get all the site collections in a Tenant

#Get All Site collections - Exclude: Seach Center, Mysite Host, App Catalog, Redirect, Content Type Hub, eDiscovery and Bot Sites

$SiteCollections = Get-PnPTenantSite | Where -Property Template -NotIn ("SRCHCEN#0", "REDIRECTSITE#0", "SPSMSITEHOST#0", "APPCATALOG#0", "POINTPUBLISHINGHUB#0", "EDISC#0", "STS#-1")

  • Use For Each to apply on each site in the site collection

#Apply to each site collection
ForEach($Site in $SiteCollections) 
{}

  • Use try to monitor the errors

#Monitor the errors
try{}

  • Connect PnP Online with the Site URL to get the details

#Connect PnPOnline With Site Url
Connect-PnPOnline -Url $Site.Url -Credentials $credentials

  • Get the Site Collections Administrators data and Initialize variables.

#Get Site Collection Administrators Data and Initialize variables
$SiteAdminstrators = @()
$SiteAdminstratorsName = @()

  • Get the Site Collection Administrator’s Email ID and Display Name.

#Get Site Collection Admin's Email and DisplayName
Get-PnPSiteCollectionAdmin -PipelineVariable Admin | ForEach-Object {
$SiteAdminstrators += $Admin.Email
$SiteAdminstratorsName += $Admin.Title }

  • Use “;” separator among the data.

#Seperate the data with ";"
$Adminstrators = ($SiteAdminstrators | select -Unique) -join ";"
$AdminstratorsName = ($SiteAdminstratorsName | select -Unique) -join ";"

  • Set variables to export to the same in CSV

#Collection Site Admin Details
$SiteAdminData += New-Object PSObject -Property @{
'Site Name' = $Site.Title
'URL' = $Site.Url
'Site Collection Admins Email' = $Adminstrators
'Site Collection Admins' = $AdminstratorsName }

  • Handle the errors from the above try block

#Handle the errors     
catch {}

  • Use the Site Collection Administrators data to expert in CSV.

#Use Site Collection Admin Data
$SiteAdminData

  • Export the required data in the CSV.

#Export the data to CSV
$SiteAdminData | Export-Csv $ReportOutput -NoTypeInformation

  • Print the data exported in CSV message.

#Print the exported message
Write-Host -f Green "Site Collection Administrators Data Exported to CSV!"

  • Catch the errors from the main Try block

#Catch the errors
Catch {}

  • Print the error details.

#Print the error details
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red

Complete PowerShell Script

<#
=====================================================================================================================================================
Name:           Get Site Collection Administrators in SharePoint Online and Export to CSV through PowerShell
Description:    This script helps to Get Site Collection Administrators in SharePoint Online and Export to CSV in an Organization through PowerShell
Version:        1.0
=====================================================================================================================================================
#>

#Install PnP PowerShell if its not installed
#Install-Module PnP.PowerShell

#Import Module
#Import-Module PnP.PowerShell

#Set Variables
$AdminSiteURL = "https://osinfotech-admin.sharepoint.com/"
$credentials=Get-Credential
$ReportOutput = "C:\SiteAdmins.csv"
 
#Monitor the errors
Try {
    #Initialize variable
    $SiteAdminData = @()

    #Connect PnPOnline with AdminSiteUrl
    Connect-PnPOnline -Url $AdminSiteURL -Credentials $credentials
   
    #Get All Site collections - Exclude: Seach Center, Mysite Host, App Catalog, Redirect, Content Type Hub, eDiscovery and Bot Sites
    $SiteCollections = Get-PnPTenantSite | Where -Property Template -NotIn ("SRCHCEN#0", "REDIRECTSITE#0", "SPSMSITEHOST#0", "APPCATALOG#0", "POINTPUBLISHINGHUB#0", "EDISC#0", "STS#-1")
    
    #Apply to each site collection
    ForEach($Site in $SiteCollections) 
    {   
    #Monitor the errors
    try{
        #Print Site Url
        Write-host -F Yellow "Getting Site Collection Administrators of the site: " $Site.Url 
        
        #Connect PnPOnline With Site Url
        Connect-PnPOnline -Url $Site.Url -Credentials $credentials
     
        #Get Site Collection Administrators Data and Initialize variables
        $SiteAdminstrators = @()
        $SiteAdminstratorsName = @()

        #Get Site Collection Admin's Email and DisplayName
        Get-PnPSiteCollectionAdmin -PipelineVariable Admin | ForEach-Object {
        $SiteAdminstrators += $Admin.Email
        $SiteAdminstratorsName += $Admin.Title }

        #Seperate the data with ";"
        $Adminstrators = ($SiteAdminstrators | select -Unique) -join ";"
        $AdminstratorsName = ($SiteAdminstratorsName | select -Unique) -join ";"
 
        #Collection Site Admin Details
        $SiteAdminData += New-Object PSObject -Property @{
        'Site Name' = $Site.Title
        'URL' = $Site.Url
        'Site Collection Admins Email' = $Adminstrators
        'Site Collection Admins' = $AdminstratorsName }    
        }#try

    #Handle the errors     
    catch {} #catch
         
    } #ForEach

    #Use Site Collection Admin Data
    $SiteAdminData
    
    #Export the data to CSV
    $SiteAdminData | Export-Csv $ReportOutput -NoTypeInformation

    #Print the exported message
    Write-Host -f Green "Site Collection Administrators Data Exported to CSV!"

    } #Try

#Handle the errors
Catch { 
       #Print the error details
       write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
       } #Catch

#Freeing resources
Finally {
        $SiteAdminstrators = ""
        $SiteAdminstratorsName = ""
        } #Finally

.

Hope this article will help us to Get Site Collection Administrators and export to CSV file through PowerShell.

Also get my article updates on my social media handles.

LinkedIn – https://www.linkedin.com/in/khasim-shaik-8784a1232/

Twitter – https://twitter.com/KhasimShaik2009

Facebook – https://www.facebook.com/profile.php?id=100078255554660

Thank you for your support, will catch up with new article soon.

Keep learning and keep smiling.

Thanks.

Khasim Shaik

SharePoint & Power Platform Developer at OS InfoTech

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: