You can use PnP PowerSHell to retrieve library permissions.
Here is a script which can do this, you just need to enter your username, site URL, and document library for which you want to get permissions. Script gets all the users, SharePoint groups and members of the SharePoint groups and permission assigned to them.
# install PnP module
Install-Module SharePointPnPPowerShellOnline
# variables to define
$username = "yourUsername"
$siteUrl = "https://companyName.sharepoint.com/sites/test"
$outputFile = "C:\LibraryPermissions.csv"
$libraryName = "Shared Library"
# connect to SP online site collection
$credential = Get-Credential -UserName $username -Message "Type the password:"
Connect-PnPOnline -Url $siteUrl -Credentials $credential
# output file name and location
if (Test-Path $OutputReport)
{
Remove-Item $OutputReport
}
"Title `t LoginName `t PrincipalType `t Permission `t GivenThrough" | Out-File $outputFile -Append
# get document library
$library = Get-PnpList -Identity $libraryName -Includes RoleAssignments
# get all the users and groups who has access
$roleAssignments = $library.RoleAssignments
foreach ($roleAssignment in $roleAssignments)
{
Get-PnPProperty -ClientObject $roleAssignment -Property RoleDefinitionBindings, Member
$loginName = $roleAssignment.Member.LoginName
$title = $roleAssignment.Member.Title
$principalType = $roleAssignment.Member.PrincipalType
$givenThrough = ""
$permissionLevel = ""
# loop through permission levels assigned to specific user/group
foreach ($roleDefinition in $roleAssignment.RoleDefinitionBindings){
$PermissionLevel += $RoleDefinition.Name + ";"
}
$givenThrough = "Given directly"
"$($title) `t $($loginName) `t $($principalType) `t $($permissionLevel) `t $($givenThrough)" | Out-File $outputFile -Append
# if principal is SharePoint group -> get SharePoint group members
if ($roleAssignment.Member.PrincipalType.ToString() -eq "SharePointGroup")
{
$givenThrough = $roleAssignment.Member.Title.ToString()
$groupMembers = Get-PnpGroupMembers -Identity $roleAssignment.Member.LoginName
foreach ($member in $groupMembers)
{
"$($member.Title) `t $($member.LoginName) `t $($member.PrincipalType) `t $($permissionLevel) `t $($title)" | Out-File $outputFile -Append
}
}
}
Please note that script does not expand Security Groups and their members.
We have a tool SysKit Security Manager which shows the information you wanted, it has Permissions Matrix report which shows who has access to document library and its' files. Hope you'll find this helpful.