Sharepoint site members list

Ameer 16 Reputation points
2024-02-27T10:28:07.54+00:00

Dear All, I am running the below script to get the list of members of my SharePoint sites in my tenant and I am getting the result. But most of the users are doesn't have the access for the site but its showing as member. $members = Get-SPOUser -Site $siteUrl | Where-Object { $_.IsSiteAdmin -eq $false } # Add members to site information array foreach ($member in $members) { $siteInfo += [PSCustomObject]@{ 'Site Collection URL' = $siteCollectionUrl 'Site URL' = $siteUrl 'Members' = $member.LoginName if someone please advice what I am going wrong.

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

2 answers

Sort by: Most helpful
  1. Yanli Jiang - MSFT 31,606 Reputation points Microsoft External Staff
    2024-02-28T05:57:47.9033333+00:00

    Hi @Ameer ,

    The script is retrieving all users from the site, including those who are not members. To get only the list of members, you can use the Get-SPOGroup cmdlet to retrieve the Members group and then get the users from that group.

    Please try this:

    # Connect to SharePoint
    Connect-SPOService -Url https://contoso-admin.sharepoint.com
    
    # Get the Members group
    $membersGroup = Get-SPOGroup -Site $siteUrl -Group "Members"
    
    # Get the users from the Members group
    $members = Get-SPOUser -Site $siteUrl -Group $membersGroup.Id
    
    # Add members to site information array
    foreach ($member in $members) {
        $siteInfo += [PSCustomObject]@{
            'Site Collection URL' = $siteCollectionUrl
            'Site URL' = $siteUrl
            'Members' = $member.LoginName
        }
    }
    

    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.


  2. Yanli Jiang - MSFT 31,606 Reputation points Microsoft External Staff
    2024-02-28T09:22:39.2966667+00:00

    Hi @Ameer , Please try this:

    # Connect to SharePoint Online
    Connect-SPOService -Url https://mydoamin-admin.sharepoint.com
    # Create an empty array to store site information
    $siteInfo = @()
    # Get all site collections
    $siteCollections = Get-SPOSite -Limit All
    # Loop through each site collection
    foreach ($siteCollection in $siteCollections) {
        $siteCollectionUrl = $siteCollection.Url
        Write-Host "Site Collection URL: $siteCollectionUrl"
        # Get all sites within the site collection
        $sites = Get-SPOSite -Limit All -Filter "Url -like '$siteCollectionUrl*'"
        # Loop through each site
        foreach ($site in $sites) {
            $siteUrl = $site.Url
            Write-Host "Site URL: $siteUrl"
            # Get site members
            $members = Get-SPOSiteGroup -Site $siteUrl | Where-Object { $_.Title -eq "Members" }
            $memberUsers = Get-SPOSiteGroup -Site $siteUrl | Where-Object { $_.Title -eq "Members" } | Select-Object -ExpandProperty Users
            
            # Add members to site information array
            foreach ($member in $memberUsers) {
                $siteInfo += [PSCustomObject]@{
                    'Site Collection URL' = $siteCollectionUrl
                    'Site URL' = $siteUrl
                    'Members' = $member.LoginName
                    
                }
            }
            # Get site owners
            $owners = Get-SPOSiteGroup -Site $siteUrl | Where-Object { $_.Title -eq "Owners" }
            $ownerUsers = Get-SPOSiteGroup -Site $siteUrl | Where-Object { $_.Title -eq "Owners" } | Select-Object -ExpandProperty Users
         
            # Add owners to site information array
            foreach ($owner in $ownerUsers) {
                $siteInfo += [PSCustomObject]@{
                    'Site Collection URL' = $siteCollectionUrl
                    'Site URL' = $siteUrl
                    'Owners' = $owner.LoginName
                    
                }
            }
        }
    }
    # Display site information
    $siteInfo | Format-Table -AutoSize
    # Export site information to CSV files for members and owners
    $siteInfo | Where-Object { $_.Members -ne $null } | Select-Object 'Site Collection URL', 'Site URL', 'Members', 'Permissions' | Export-Csv -Path ".\SharePoint\SharePoint_Members.csv" -NoTypeInformation -Encoding UTF8
    $siteInfo | Where-Object { $_.Owners -ne $null } | Select-Object 'Site Collection URL', 'Site URL', 'Owners', 'Permissions' | Export-Csv -Path ".\SharePoint\SharePoint_Owners.csv" -NoTypeInformation -Encoding UTF8
    

    This modified script uses the Get-SPOSiteGroup cmdlet to retrieve the "Members" and "Owners" groups for each site, and then uses the Select-Object cmdlet with the -ExpandProperty parameter to retrieve the users in those groups. It then adds the user to the site information array.


    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.