In my case, I had to use it an OneDrive Site Collection but it also works for any other site.
While working with OneDrive you should pay attention to the Permissions in the Site Collection as by default you will probably not have access to it.
$foldersUniquePermissions = @()
$filesUniquePermissions = @()
foreach($doc in $documents){
$context.Load($doc)
switch($doc.FileSystemObjectType){
'Folder'{
$folder = Get-PnPFolder -Url ($doc.FieldValues.FileRef).Substring(37)
-Includes ListItemAllFields.RoleAssignments, ListItemAllFields.HasUniqueRoleAssignments
if($folder.ListItemAllFields.HasUniqueRoleAssignments){
$foldersUnique += $doc
}
}
'File'{
$file = Get-PnPFile -Url ($doc.FieldValues.FileRef).Substring(37) -AsListItem
Get-PnPProperty -ClientObject $file -Property HasUniqueRoleAssignments, RoleAssignments
if($file.HasUniqueRoleAssignments -eq $True){
$filesUnique += $doc
}
}
}
}
Posted Here:https://sharepoint.stackexchange.com/questions/221118/retrieve-permissions-at-folder-and-file-level-in-powershell/268867#268867