PowerShell Collect permission or share from a document in SharePoint online

nellie 126 Reputation points
2024-08-05T13:40:01.1866667+00:00

Is there a PowerShell which looks at a document and list all users accessing it ?

If so what is the PowerShell ?

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

2 answers

Sort by: Most helpful
  1. Ling Zhou_MSFT 23,620 Reputation points Microsoft External Staff
    2024-08-06T02:19:34.4333333+00:00

    Hi @nellie,

    Thank you for posting in this community.

    We can use PnP PowerShell to list all visitors to a file and their permissions.

    1.Install PnP PowerShell Module.

    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. 

    2.Run the following PnP PowerShell. Don't forget to change the parameter to your file path.

    #Function to Get Permissions Applied on a particular Object such as: Web, List, Library, Folder or List Item
    Function Get-PnPPermissions([Microsoft.SharePoint.Client.SecurableObject]$Object)
    {
        Try {
            #Get permissions assigned to the Folder
            Get-PnPProperty -ClientObject $Object -Property HasUniqueRoleAssignments, RoleAssignments
     
            #Check if Object has unique permissions
            $HasUniquePermissions = $Object.HasUniqueRoleAssignments
        
            #Loop through each permission assigned and extract details
            $PermissionCollection = @()
            Foreach($RoleAssignment in $Object.RoleAssignments)
            {
                #Get the Permission Levels assigned and Member
                Get-PnPProperty -ClientObject $RoleAssignment -Property RoleDefinitionBindings, Member
        
                #Get the Principal Type: User, SP Group, AD Group
                $PermissionType = $RoleAssignment.Member.PrincipalType
                $PermissionLevels = $RoleAssignment.RoleDefinitionBindings | Select -ExpandProperty Name
     
                #Remove Limited Access
                $PermissionLevels = ($PermissionLevels | Where { $_ -ne "Limited Access"}) -join ","
                If($PermissionLevels.Length -eq 0) {Continue}
     
                #Get SharePoint group members
                If($PermissionType -eq "SharePointGroup")
                {
                    #Get Group Members
                    $GroupMembers = Get-PnPGroupMember -Identity $RoleAssignment.Member.LoginName
                     
                    #Leave Empty Groups
                    If($GroupMembers.count -eq 0){Continue}
     
                    ForEach($User in $GroupMembers)
                    {
                        #Add the Data to Object
                        $Permissions = New-Object PSObject
                        $Permissions | Add-Member NoteProperty User($User.Title)
                        $Permissions | Add-Member NoteProperty Type($PermissionType)
                        $Permissions | Add-Member NoteProperty Permissions($PermissionLevels)
                        $Permissions | Add-Member NoteProperty GrantedThrough("SharePoint Group: $($RoleAssignment.Member.LoginName)")
                        $PermissionCollection += $Permissions
                    }
                }
                Else
                {
                    #Add the Data to Object
                    $Permissions = New-Object PSObject
                    $Permissions | Add-Member NoteProperty User($RoleAssignment.Member.Title)
                    $Permissions | Add-Member NoteProperty Type($PermissionType)
                    $Permissions | Add-Member NoteProperty Permissions($PermissionLevels)
                    $Permissions | Add-Member NoteProperty GrantedThrough("Direct Permissions")
                    $PermissionCollection += $Permissions
                }
            }
            #Export Permissions to CSV File
            $PermissionCollection | Export-CSV $ReportFile -NoTypeInformation
            Write-host -f Green "`n*** File Permission Report Generated Successfully!***"
        }
        Catch {
        write-host -f Red "Error Generating File Permission Report!" $_.Exception.Message
        }
    }
       
    #region ***Parameters***
    $SiteURL="https://tenant.sharepoint.com/sites/24July"
    $ReportFile="C:\FilePermissionRpt.csv"
    $FileRelativeURL = "/sites/24July/Shared%20Documents/rose1.docx"
    #endregion
     
    #Connect to the Site collection
    Connect-PnPOnline -URL $SiteURL -Interactive
     
    #Get the Folder from URL
    $File = Get-PnPFile -Url $FileRelativeURL
     
    #Call the function to generate permission report
    Get-PnPPermissions $File.ListItemAllFields
    

    If you want to get a report on who has accessed the file over a certain period of time, you can do so in this article: SharePoint Online: How to See Who has viewed a File?

    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.


  2. Alvaro Avila 170 Reputation points
    2024-09-12T10:25:18.03+00:00

    You can you this free and open-source app which can generate a full site permissions report: https://github.com/Barbarur/NovaPoint

    0 comments No comments

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.