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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.