PS script to check what sites the user has access to

SR VSP 1,251 Reputation points
2023-05-04T12:14:04.3366667+00:00

Hi Guys,

I'm looking for a PS script to check what sites the user has access to across the teanant in SharePoint Online. Any help ?

Ex: If we input a user email id that should give all the sites the user has access to

@Jerry Xu-MSFT

Microsoft 365 and Office SharePoint Development
Microsoft 365 and Office SharePoint For business Windows
0 comments No comments
{count} vote

2 answers

Sort by: Most helpful
  1. Tasadduq Burney 8,956 Reputation points MVP Volunteer Moderator
    2023-05-04T12:28:52.4333333+00:00

    Here's a PowerShell script that should give you the list of all sites the user has access to in SharePoint Online:

    # Connect to SharePoint
    Connect-SPOService -Url https://contoso-admin.sharepoint.com
    
    # Set the user's email address
    $userEmail = "user@example.com"
    
    # Get all site collections in the tenant
    $sites = Get-SPOSite -Limit All
    
    # Loop through each collection and check if the user has access
    foreach ($site in $sites) {
    
        # Get the user's permissions for the site collection
    
        $permissions = Get-SPOUser -Site $site.Url -LoginName $userEmail -ErrorAction SilentlyContinue
        if ($permissions) {
    
            # If the user has permissions, output the site collection URL
    
            Write-Host "User $($userEmail) has access to $($site.Url)"
        }
    }
    
    1 person found this answer helpful.

  2. Xyza Xue_MSFT 30,176 Reputation points Microsoft External Staff
    2023-05-05T07:58:31.5066667+00:00

    Hi @Srini VSP ,

    As far as I know this shouldn't be possible using Powershell. It’s very easy to export “who has access to this particular resource” but much more difficult to export “what resources does specific have access to?”

    I tested the code provided by Tasadduq Burney , and the output of this PS code seems to show that I have access to all site urls in the teanant (as shown in the figure below, it shows that I have access to the site "test1232", but actually I cannot access ). Even I can help you to modify the PS code to make it output to csv file.

    User's image

    I found a discussion with the same requirement as yours ( Possibly achievable via MS Graph or other methods), you can check it out:

    https://www.reddit.com/r/sharepoint/comments/10eob9v/list_all_sites_a_user_has_access_to/

    Maybe you can take a look at this, this approach also meets your needs.: Use Search to List All the Sites You Have Access
    Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link.


    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.