Using PowerShell, can I list all members, visitors, and guests of a SharePoint site?

Bailey, John 0 Reputation points
2023-06-19T21:05:20.99+00:00

Can someone help me with a PowerShell script that can list all members, visitors, and guests of a particular SharePoint site? Would this need to be done in more than one command?

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,301 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,331 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Limitless Technology 44,126 Reputation points
    2023-06-20T20:38:29.21+00:00

    Hello there,

    You can use the ‘Get-SPOExternalUser’ or ‘Get-SPOUser’ to get the external users. With ‘Get-SPOExternalUser’, you can get only 50 external users by default. ‘Get-SPOUser’ will list all the external users invited to SharePoint sites. But you need to run this cmdlet with each site URL to get the complete list.

    Before we start to manage users and groups, you need to get lists of your sites, groups, and users. You can then use this information to work through the example in this article.

    https://learn.microsoft.com/en-us/microsoft-365/enterprise/manage-sharepoint-users-and-groups-with-powershell?view=o365-worldwide

    Hope this resolves your Query !!

    --If the reply is helpful, please Upvote and Accept it as an answer--

    0 comments No comments

  2. Zehui Yao_MSFT 5,846 Reputation points
    2023-06-27T07:16:01.6433333+00:00

    Hi Bailey, John, for members, visitors. You can access the groups of members and visitors through the Get-SPOSiteGroup cmdlet and get the users in them. The following is a script for reference:

    #Import SharePoint Online Management Shell
    Import-Module Microsoft.Online.Sharepoint.PowerShell -DisableNameChecking
     
    #Variables for processing
    $AdminURL = "https://crescent-admin.sharepoint.com/"
    $AdminName = "spadmin@crescent.com"
    $SiteURL = "https://crescent.sharepoint.com/sites/sales"
    $CSVPath = "C:\Users\Administrator\Desktop\GroupsReport.csv"
      
    #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 $Credentialexp
    
    $GroupsData = @()
     
    #get all sharepoint online groups
    $SiteGroups = Get-SPOSiteGroup -Site $SiteURL
     
    #Get Members of each group
    foreach($Group in $SiteGroups)
    {
        Write-host "Group:"$Group.Title
        
        Get-SPOSiteGroup -Site $SiteURL -Group $Group.Title | Select-Object -ExpandProperty Users
        
        $GroupsData += New-Object PSObject -Property @{
            'Site URL' = $SiteURL
            'Group Name' = $Group.Title
            'Users' = $Group.Users -join ","
        }
    }
    
    $GroupsData | Export-Csv $CSVPath -NoTypeInformation
    

    Screenshot 2023-06-27 160742

    And for guests, you can use the cmdlet Get-SPOExternalUser:

    #Import SharePoint Online Management Shell
    Import-Module Microsoft.Online.Sharepoint.PowerShell -DisableNameChecking
     
    #Variables for processing
    $AdminURL = "https://crescent-admin.sharepoint.com/"
    $AdminName = "spadmin@crescent.com"
    $SiteURL="https://crescent.sharepoint.com/sites/sales"
    $CSVPath = "C:\Users\Administrator\Desktop\GuestsReport.csv"
      
    #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 $Credentialexp
    
    $GroupsData = @()
     
    #get all sharepoint online groups
    $Guests = Get-SPOExternalUser
     
    #Get Members of each group
    foreach($Guest in $Guests)
    {
        $GroupsData += New-Object PSObject -Property @{
            'Site URL' = $SiteURL
            'Guest' = $Guest.DisplayName
        }
    }
    
    $GroupsData | Export-Csv $CSVPath -NoTypeInformation
    
    
    

    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.


  3. Zen ul Bashar 0 Reputation points
    2024-07-25T01:48:09.09+00:00

    I'm trying to retrieve all SharePoint site groups along with their respective owners, members, visitors, and guests. While I can see visitors from the admin center and SharePoint admin center, my script encounters "access denied" errors when I run it. Interestingly, the script works perfectly on a test site where I've given our admin account site admin access.

    Is there a different parameter I should be using, or is my approach incorrect for retrieving visitors from Teams-enabled sites? Any guidance or suggestions would be greatly appreciated!

    Thanks in advance! I've put the question in another thread. https://techcommunity.microsoft.com/t5/microsoft-teams/retrieving-sharepoint-site-visitors-using-powershell-script/m-p/4198417/highlight/true#M134924