SharePoint Online PowerShell Script Creation for Lists and Permissions
by Hozafa Motiwala
Here's a SharePoint Online PowerShell script for creating Lists and Permissions. The text that reads 'TEST' will need to be updated to the name of the list name or group name.
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true, HelpMessage="Enter the URL of the SharePoint site")]
[String]
$SiteUrl
)
Connect-PnPOnline -Url $SiteUrl
#Create list
New-PnPList -Title 'TEST' -Url "Lists/TEST" -Template GenericList
Write-Host "TEST list successfully created" -ForegroundColor Green
#Create custom Permission levels
$editRole = Get-PnPRoleDefinition -Identity "Contribute"
Add-PnPRoleDefinition -RoleName "TEST Contribute" -Clone $editRole -Exclude DeleteListItems, EditListItems, DeleteVersions -Description "TEST Custom Contribute (edit or delete prohibited) set by Site Provisioning"
Write-Host "Contribute role cloned successfully" -ForegroundColor Green
$readRole = Get-PnPRoleDefinition -Identity "Read"
Add-PnPRoleDefinition -RoleName "TEST Read" -Clone $readRole -Include AddListItems -Description "TEST Custom Read (with ability to add) set by Site Provisioning"
Write-Host "Read role cloned successfully" -ForegroundColor Green
#Break inheritance on TEST list
$TESTList = Get-PnPList " TEST"
$TESTList.BreakRoleInheritance($true, $true)
$TESTList.Update()
$TESTList.Context.Load($TESTList)
$TESTList.Context.ExecuteQuery()
Write-Host "TEST list role inheritance broken" -ForegroundColor Green
#Apply new custom permissions to group
$memGroup = Get-PnPGroup -AssociatedMemberGroup
$memGroup = $memGroup.Title
Set-PnPGroupPermissions -List "TEST" -Identity $memGroup -RemoveRole 'Edit' -AddRole 'TEST Contribute'
Write-Host "TEST Members role successfully adjusted" -ForegroundColor Green
$visitorsGroup = Get-PnPGroup -AssociatedVisitorGroup
$visitorsGroup = $visitorsGroup.Title
Set-PnPGroupPermissions -List "TEST" -Identity $visitorsGroup -RemoveRole 'Read' -AddRole 'TEST Read'
Write-Host "TEST Visitors role successfully adjusted" -ForegroundColor Green