PowerShell script to retrieve Groups, Users, Permissions and URL from Site & its Sub-Sites in SharePoint Online

function connect-site($webs,$cred){
	Connect-PnPOnline -Url $webs -Interactive
}

function get-sitepermission($web,$cred){
$rec=@();
connect-site -webs $web 
if($web -eq $parentsitename){ 
	#Write-Host "Parent site permission" $web 
	$Pgroups=Get-PNPGroup -Includes Users
	foreach($Pgroup in $Pgroups){ 
		$DLGP = "" | Select "SiteUrl","GroupName","Permission","Users" 
		$pPerm=Get-PNPGroupPermissions -Identity $Pgroup.loginname -ErrorAction SilentlyContinue |Where-Object {$_.Hidden -like "False"} 
		
		if($pPerm -ne $null){ 
			$DLGP.SiteUrl=$web 
			#write-host $DLGP.SiteUrl
			$DLGP.GroupName=$Pgroup.loginname 
			#write-host $DLGP.GroupName
			$DLGP.Permission=$pPerm.Name 
			$DLGP.Users = $Pgroup.Users.Title -join ";"
			#write-host $DLGP.Permission
			
			$rec+= $DLGP 
		} 
	} 
} 
$subwebs=Get-PNPSubWebs 
foreach($subweb in $subwebs){ 
	connect-site -webs $subweb.Url  
	#Write-Host $subweb.Url 
	$groups=Get-PNPGroup -Includes Users
	foreach($group in $groups){ 
		$DLGP = "" | Select "SiteUrl","GroupName","Permission","Users" 
		$sPerm=Get-PNPGroupPermissions -Identity $group.loginname -ErrorAction SilentlyContinue |Where-Object {$_.Hidden -like "False"} 
		if ($sPerm -ne $null){ 
			$DLGP.SiteUrl=$subweb.Url 
			#write-host $DLGP.SiteUrl
			$DLGP.GroupName=$group.loginname 
			#write-host $DLGP.GroupName
			$DLGP.Permission=$sPerm.Name 
			#write-host $DLGP.Permission
			$DLGP.Users= $group.Users.Title -join ";"
			$rec+=$DLGP 
		} 
} 
Write-Host $subweb.Url "permission fetched!" 
get-sitepermission -web $subweb.Url 
} 
return $rec 
} 
#Input parameter 
#$cred=Get-Credential 
Write-Host "Enter wilcard"
$wild = Read-Host 
Write-Host "Enter Site Name"
$sitename = Read-Host
$parentsitename="https://xyz.sharepoint.com/"+$wild+"/" +$sitename
$outputPath= "C:\xyz\abc\"+ $sitename +"groups permissions.csv" 
#Write-Host $parentsitename
$Sitepermission=get-sitepermission -web $parentsitename  -Interactive
$Sitepermission |Export-Csv -Path $outputPath

Usage:

C:\xyz\abc> .\getthisscript.ps1

Enter wildcard

sites

Enter Site Name

mySite

https://abc.sharepoint.com/sites/mySite/who permission fetched!

c:\xyz\abc\mySitegroups permissions.csv

SiteUrl

https://abc.sharepoint.com/sites/mySite/who
GroupName

myGroup
Permission
Full Control
Users
She Her; He Him; Them They;

This is a continuation/add on to technet article: https://social.technet.microsoft.com/Forums/en-US/77a8e9ef-56a5-47e4-9b54-209f37687013/sharepoint-online-subsite-permissions-list-credentials-promt

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s