Is there a sharepoint online module that will allow to get site admin, members and guest through powershell?

Jayron Cena 5 Reputation points
2023-10-25T09:53:02.1066667+00:00

I have a sharepoint admin access and I have the list of all site URLs in our tenant.

I want to get all the admins, members and guests of each site through powershell script.

User's image

I tried using Get-SPOUser module to get the above-mentioned users, but the module only works if I am an owner, admin or member of the site.

Is there a powershell module or any way to get all the users(admins,members and guest) of all sharepoint sites?

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

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 40,481 Reputation points Microsoft External Staff
    2023-10-26T02:08:05.8166667+00:00

    Hi @Jayron Cena,

    Per my research, there is no straightforward ways of getting all user profiles within SharePoint tenant using SharePoint Online CSOM API. But you could consider the following approach:

    Here is a sample for you to reference

    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.UserProfiles.dll"
    
    
    
    Function Get-SPOContext([string]$Url,[string]$UserName,[string]$Password)
    {
       $context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
       $context.Credentials = Get-SPOCredentials -UserName $UserName -Password $Password
       return $context
    }
    
    
    Function Get-SPOCredentials([string]$UserName,[string]$Password)
    {
       $SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
       return New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
    }
    
    
    
    Function Print-UserProfileInfo([Microsoft.SharePoint.Client.UserProfiles.PeopleManager]$PeopleManager,[string]$AccountName){
       $ctx = $PeopleManager.Context
       $accountName = "i:0#.f|membership|" + $AccountName  #claim format  
       $userProfile = $PeopleManager.GetPropertiesFor($AccountName)
       $ctx.Load($userProfile)
       $ctx.ExecuteQuery()
       Write-Host $userProfile.PersonalUrl
    }
    
    
    
    
    $tenantUrl = "https://contoso.sharepoint.com/"
    $userName = "******@contoso.onmicrosoft.com" 
    $password = "password"
    
    $secPassword = ConvertTo-SecureString $password -AsPlainText -Force
    $cred = New-Object System.Management.Automation.PSCredential ($userName, $secPassword)
    Connect-MsolService -Credential $cred
    $allUsers = Get-MsolUser
    
    
    
    
    $Context = Get-SPOContext -Url $tenantUrl -UserName $userName -Password $password
    $peopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($Context)
    $allUsers |  % { Print-UserProfileInfo -PeopleManager $peopleManager -AccountName $_.UserPrincipalName }
    $Context.Dispose()
    

    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.