How to automatically remove access from certain subfolders within a SharePoint document library

Austin Admin 1 Reputation point

Hi All,

I am trying to automatically remove access from subfolders that are located in a "Job Folder" in a document library.


Here is the folder that we use as a template to make more "Jobs"

Inside this folder are 4 subfolders


The access to the bottom two folders needs to remain the same because everyone needs to access those folders, but for every "Job'" we need to remove site members and visitors from being able to access the top two folders.

Is there any way to automate this instead of going into each "Job" and removing the access from the two top folders? I have tried to use desktop flows in power automate but they are not consistent when removing access.

A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,520 questions
SharePoint Workflow
SharePoint Workflow
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Workflow: An orchestrated and repeatable pattern of business activity, enabling data transformation, service provision, and information retrieval.
503 questions
SharePoint Server Management
SharePoint Server Management
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Management: The act or process of organizing, handling, directing or controlling something.
2,789 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. CaseyYang-MSFT 10,316 Reputation points

    Hi @Austin Admin ,

    You could remove group from Folder Permissions using PowerShell as a workaround.

    PowerShell commands:

    #Load SharePoint CSOM Assemblies  
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"  
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  
    Function Remove-SPOGroupPermissionsFromList()  
            [Parameter(Mandatory=$true)] [string] $SiteURL,  
            [Parameter(Mandatory=$true)] [string] $FolderURL,  
            [Parameter(Mandatory=$true)] [string] $GroupName  
        Try {  
            #Get credentials to connect  
            $Cred= Get-Credential  
            $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)  
            #Setup the context  
            $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)  
            $Ctx.Credentials = $Credentials  
            $Web = $Ctx.web  
            #Get the Folder  
            $Folder = $Web.GetFolderByServerRelativeUrl($FolderURL)  
            #Break Permission inheritence - Keep all existing list permissions & Don't keep Item level permissions  
            Write-host -f Yellow "Folder's Permission inheritance broken..."  
            #Get the SharePoint Site Group object  
            $Group =$Web.SiteGroups.GetByName($GroupName)  
            #Get permissions assigned to the folder  
            #Check if the Group has permission on the list  
            [Bool]$GroupFound = $False  
            ForEach($RoleAssignment in $Folder.ListItemAllFields.RoleAssignments)  
                #remove Group permission from folder  
                If($RoleAssignment.Member.LoginName -eq $Group.LoginName)  
                    $GroupFound = $True  
                    Write-host "Group Permissions Removed from the List Successfully!" -ForegroundColor Green   
            #If Group doesn't exist in list permissions  
            If($GroupFound -eq $False) { Write-host "Group Not found in List Permissions!" -ForegroundColor Red}  
        Catch {  
           write-host -f Red "Error Removing Group permissions from the Folder!" $_.Exception.Message  
    #Config Variables  
    #Call the function to remove Group permissions from a list  
    Remove-SPOGroupPermissionsFromList -SiteURL $SiteURL -FolderURL $FolderURL -GroupName $GroupName  

    For Reference: Remove Group from Folder Permissions using PowerShell
    Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link.

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.