How to check all SharePoint Site Owners User usnig CSOM PowerShell?

Sayantan Ganguly 80 Reputation points
2023-11-02T11:05:24.7666667+00:00

Hello Team,

I am SharePoint Admin and able to see all SiteOwners, Members Users from admin centre. But while trying to get the SiteOwners using CSOM PowerSHell I am not able to get and getting 401 for the sites where i dont have access.

How to get all of the site owners using CSOM PowerShell whether I have access to the site or not? I have admin permissions.

Thanks in advance

Microsoft 365 and Office | SharePoint | Development
Microsoft 365 and Office | SharePoint | For business | Windows
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2023-11-03T02:04:14.68+00:00

    Hi @Sayantan Ganguly,

    Per my test, I could get site owners by following powershell script

    #Variables for Admin Center & Site Collection URL
    $AdminCenterURL = "https://tenant-admin.sharepoint.com"
    
     
    #Connect to SharePoint Online
    Connect-SPOService -url $AdminCenterURL -Credential (Get-Credential)
     
    #sharepoint online powershell get site owner
    Get-SPOSite -limit ALL | Select URL, Owner
    

    After running the script we will need enter username and password in the window

    User's image

    And we will get following result

    User's image


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Yanli Jiang - MSFT 31,606 Reputation points Microsoft External Staff
    2023-11-03T08:42:42.7166667+00:00

    Hi @Sayantan Ganguly,

    You can try this PowerShell script to get site owners for all sites in the tenant:

    #Variables for Admin Center
    $AdminCenterURL = "https://tenant-admin.sharepoint.com"
    $CSVPath = "C:\Users\spadmin\Desktop\siteowners.csv"
    
    $username = "******@tenant.onmicrosoft.com"
    $password = "password"
    $cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $userName, $(convertto-securestring $Password -asplaintext -force)
    
     
    #Connect to SharePoint Online and Azure AD
    Connect-SPOService -url $AdminCenterURL -Credential $Cred
    Connect-AzureAD -Credential $Cred | Out-Null
       
    #Get all Site Collections
    $Sites = Get-SPOSite -Limit ALL
     
    $SiteOwners = @()
    #Get Site Owners for each site collection
    $Sites | ForEach-Object {
        If($_.Template -like 'GROUP*')
        {
            $Site = Get-SPOSite -Identity $_.URL
            #Get Group Owners
            $GroupOwners = (Get-AzureADGroupOwner -ObjectId $Site.GroupID | Select -ExpandProperty UserPrincipalName) -join "; "      
        }
        Else
        {
            $GroupOwners = $_.Owner
        }
        #Collect Data
        $SiteOwners += New-Object PSObject -Property @{
        'Site Title' = $_.Title
        'URL' = $_.Url
        'Owner(s)' = $GroupOwners
        }
    }
    #Get Site Owners
    $SiteOwners
     
    #Export Site Owners report to CSV
    $SiteOwners | Export-Csv -path $CSVPath -NoTypeInformation
    

    There is the result:

    11031

    For more information, please refer to:

    https://www.sharepointdiary.com/2018/02/get-sharepoint-online-site-owner-using-powershell.html


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.