0

Using PnP PowerShell, I would like to create an extra document library in SharePoint Online, where only the owner group has access. With the script below I am able to remove all groups I want to remove, except "Hub Visitors".

How can I get to the point where only the owner group has access to the document library?

$InternalDocumentsLibraryName = "Internal Docs"
New-PnPList -Title InternalDocumentsLibraryName -Template DocumentLibrary

$MemberGroupName = Get-PnPGroup -AssociatedMemberGroup
$VisitorGroupName = Get-PnPGroup -AssociatedVisitorGroup


$InternalDocuments = Get-PnPList -Identity $InternalDocumentsLibraryName
 
If($InternalDocuments)
{
       
    Set-PnPList -Identity $InternalDocumentsLibraryName -BreakRoleInheritance -CopyRoleAssignments
    Set-PnPListPermission -Identity $InternalDocumentsLibraryName -Group $MemberGroupName -RemoveRole 'Edit'
    Set-PnPListPermission -Identity $InternalDocumentsLibraryName -Group $VisitorGroupName -RemoveRole 'Read'
}
Else
{   
    Write-Host -f Yellow "Could not find '$InternalDocumentsLibraryName'"
}

2 Answers 2

0

Try using this code:

//Email address of user running PowerShell
$userEmail = "[email protected]"

$InternalDocumentsLibraryName = "Internal Docs"
New-PnPList -Title InternalDocumentsLibraryName -Template DocumentLibrary

$OwnersGroupName = Get-PnPGroup -AssociatedOwnerGroup

$InternalDocuments = Get-PnPList -Identity $InternalDocumentsLibraryName 

If($InternalDocuments)
{
       
    Set-PnPList -Identity $InternalDocumentsLibraryName -BreakRoleInheritance
    Set-PnPListPermission -Identity $InternalDocumentsLibraryName -Group $OwnersGroupName -AddRole 'Full Control'
    Set-PnPListPermission -Identity $InternalDocumentsLibraryName -User $userEmail -RemoveRole 'Full Control'
}
Else
{   
    Write-Host -f Yellow "Could not find '$InternalDocumentsLibraryName'"
}
0

The workaround is to connect the site to the hub as a last step, not as a first step. Then "Hub Visitors" will not be added, so then there is no need to remove this group.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.