Get Site Permissions with PnP PowerShell
-
07-01-2021 - |
Question
How can I retrieve site permissions on a site or subsite using PnP PowerShell?
Example: https://$orgName.sharepoint.com/sites/IT/Atlas
Via the UI: Site settings > Site Permissions
With PnP PowerShell, the following didn't work for me:
Connect-PnPOnline -Url "https://$orgName.sharepoint.com/sites/IT/Atlas" -Credentials $userCredential
$web = Get-PnPWeb -Includes RoleAssignments
[Edit] The above code actually seems to work, as $web.RoleAssignments.Count returns 5. My issue is that I don't know how to take it from here, to enumerate the 5 members and their roles.
RoleAssignments.Member returns 5 items but I can't figure out how to get the names and roles.
RoleAssignments.Groups only returns the SharePoint groups.
Solution
The question has been answered on Microsoft Tech Community.
Here is the accepted answer:
$cred = get-credential
Connect-PnPOnline -Url "https://$orgname.sharepoint.com" -Credentials $cred
$web = Get-PnPWeb -Includes RoleAssignments
foreach($ra in $web.RoleAssignments) {
$member = $ra.Member
$loginName = get-pnpproperty -ClientObject $member -Property LoginName
$rolebindings = get-pnpproperty -ClientObject $ra -Property RoleDefinitionBindings
write-host "$($loginName) - $($rolebindings.Name)"
write-host
}
OTHER TIPS
The following PowerShell code snippet assits to retrieve the site properties:
$siteurl = "https://abc.sharepoint.com"
Connect-SPOnline -Url $siteurl
$ctx = Get-SPOContext
Get Current Context Site (Root)
function RetrieveSite(){
$web = Get-SPOWeb
Write-Host "Title : " $web.Title
Write-Host "Description : " $web.Description
Write-Host "URL : " $web.Url
}
Get Sub Site
function RetrieveSubSite(){
$web = Get-SPOWeb "PnPSite1"
Write-Host "Title : " $web.Title
Write-Host "Description : " $web.Description
Write-Host "URL : " $web.Url
}
RetrieveSite #Get Current Context Site (Root)
RetrieveSubSite #Get Sub Site
Please refer to the below link
Use powershell to retrieve all subsites for sharepoint online
Hope it helps!