SharePoint Online: Get Subsite Permission Report using PowerShell

Requirement: PowerShell script to get subsite permissions in SharePoint Online

How to Get Subsite Permissions in SharePoint Online?

To get permissions of a SharePoint Online subsite,

  • Click on Setting gear >> Click on the “Site Permissions” link
  • Click on “Advanced Permission Settings” in the site permissions pagesharepoint online powershell subsite permissions

This gives you all users and groups that have permissions to the subsite

SharePoint Online: PowerShell to Get Subsite Permissions

This PowerShell script extracts and exports all direct permissions of the given subsite (not any underlying objects such as list, folder, file).

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing/2020"
$ReportOutput = "C:\Temp\SitePermissionRpt.csv"

#Connect to Site
Connect-PnPonline -Url $SiteURL -Interactive

#Get the web
$Web = Get-PnPWeb -Includes RoleAssignments

#Loop through each permission assigned and extract details
$PermissionData = @()
ForEach ($RoleAssignment in $Web.RoleAssignments)
{
    #Get the Permission Levels assigned and Member
    Get-PnPProperty -ClientObject $RoleAssignment -Property RoleDefinitionBindings, Member
    
    #Get the Permission Levels assigned
    $PermissionLevels = ($RoleAssignment.RoleDefinitionBindings | Select -ExpandProperty Name | Where {$_ -ne "Limited Access"}) -join ","
    $PermissionType = $RoleAssignment.Member.PrincipalType

    #Leave Principals with no Permissions
    If($PermissionLevels.Length -eq 0) {Continue}
    
    #Collect Permission Data
    $Permissions = New-Object PSObject
    $Permissions | Add-Member NoteProperty Name($RoleAssignment.Member.Title)
    $Permissions | Add-Member NoteProperty Type($PermissionType)
    $Permissions | Add-Member NoteProperty PermissionLevels($PermissionLevels)
    $PermissionData += $Permissions
}

$PermissionData
$PermissionData | Export-csv -path $ReportOutput -NoTypeInformation

This script gets all permissions from the subsite and generates a CSV as:

sharepoint online subsite permission report using powershell

SharePoint Online: PowerShell to Get Site Permissions

While the above script extracts permissions applied on the subsite, what if you want to get members of each group along with the direct permissions to the site?

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing/2020"
$ReportOutput = "C:\Temp\SitePermissionRpt.csv"

#Connect to Site
Connect-PnPonline -Url $SiteURL -Interactive

#Get the web
$Web = Get-PnPWeb -Includes RoleAssignments

#Loop through each permission assigned and extract details
$PermissionData = @()
ForEach ($RoleAssignment in $Web.RoleAssignments)
{
    #Get the Permission Levels assigned and Member
    Get-PnPProperty -ClientObject $RoleAssignment -Property RoleDefinitionBindings, Member
    
    #Get the Permission Levels assigned
    $PermissionLevels = ($RoleAssignment.RoleDefinitionBindings | Select -ExpandProperty Name | Where { $_ -ne "Limited Access"} ) -join ","
    
    #Leave Principals with no Permissions
    If($PermissionLevels.Length -eq 0) {Continue}

    $PermissionType = $RoleAssignment.Member.PrincipalType
    #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 }
        $GroupUsers = ($GroupMembers | Select -ExpandProperty LoginName | Where { $_ -ne "SHAREPOINT\system"}) -join "; "
  
        #Add the Data to Object
        $Permissions = New-Object PSObject
        $Permissions | Add-Member NoteProperty Name($RoleAssignment.Member.Title)
        $Permissions | Add-Member NoteProperty Accounts($GroupUsers)
        $Permissions | Add-Member NoteProperty Type($PermissionType)
        $Permissions | Add-Member NoteProperty PermissionLevels($PermissionLevels)
        $PermissionData += $Permissions
    }
    Else
    {
        #Add the Data to Object
        $Permissions = New-Object PSObject
        $Permissions | Add-Member NoteProperty Name($RoleAssignment.Member.Title)
        $Permissions | Add-Member NoteProperty Accounts($RoleAssignment.Member.LoginName)
        $Permissions | Add-Member NoteProperty Type($PermissionType)
        $Permissions | Add-Member NoteProperty PermissionLevels($PermissionLevels)
        $PermissionData += $Permissions
    }
}

#Export Permissions data to CSV file
$PermissionData | Export-csv -path $ReportOutput -NoTypeInformation

This script gets all the users, SharePoint groups and members of the SharePoint Online site or subsite along with the permissions assigned to them. Please note, this script generates a permission report of the given site or subsite and not the permissions of any underlying objects such as subsite, list, library, folder, or list item. To generate the complete permissions report for the SharePoint Online site, use: SharePoint Online: Generate Permissions Report for a Site Collection using PnP PowerShell

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

3 thoughts on “SharePoint Online: Get Subsite Permission Report using PowerShell

Leave a Reply