Powershell script to make files in a sharepoint online folder read only

Agarwal, Komal 1 Reputation point
2023-02-19T12:10:21.43+00:00

Hi,

We have a requirement to make certain files in a SharePoint online folder to read only via Powershell script. Please let me know how it can be achieved

Thanks,

Komal

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
11,230 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,863 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 39,761 Reputation points Microsoft External Staff
    2023-02-20T05:23:29.8633333+00:00

    Hi @Agarwal, Komal

    Per my research, there is no direct way to set the SharePoint folder to read-only! The read-only mode can be set only on SharePoint site collections (How to Make a Site Collection Read-Only) or on content databases (Set SharePoint Content Database to Read Only Mode). However, we can make a SharePoint library to read-only by replacing all user’s permissions into “Read-only”. The following script will restrict users from making changes to the list or library.

    Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
     
    #Configuration Parameters
    $SiteURL="https://intranet.crescent.com/"
    $ListName= "Projects"
     
    #Get the Web and List
    $Web = Get-SPWeb $SiteURL
    $List = $Web.Lists.TryGetList($ListName)
     
    #Break Permissions of the List
    If ($List.HasUniqueRoleAssignments -eq $false)
    {
        $List.BreakRoleInheritance($true)
    }
     
    #Get Read Permission Level
    $ReadPermission = $web.RoleDefinitions["Read"]
     
    #Get All User & Groups granted Permissions to the List
    ForEach ($RoleAssignment in $List.RoleAssignments)
    {
        Write-host "Resetting Permissions for :"$RoleAssignment.Member.Name -f Yellow
     
        #Replace All other permissions with "Read" if its not granted already
        $RoleDefinitionBindings = $RoleAssignment.RoleDefinitionBindings
        Foreach($RoleDefBinding in $RoleDefinitionBindings)
        {
            IF( ($RoleDefBinding.Name -ne "Read") -and ($RoleDefBinding.Name -ne "Restricted Read") -and ($RoleDefBinding.Name -ne "View Only") -and ($RoleDefBinding.Name -ne "Limited Access") )
            {
                #Grant Read ACcess if its not present
                If(!($RoleAssignment.RoleDefinitionBindings.Contains($ReadPermission)))
                {
                    $RoleAssignment.RoleDefinitionBindings.Add($ReadPermission)
                    $RoleAssignment.Update()
                    Write-host "Added Read Permissions to "$RoleAssignment.Member.Name -ForegroundColor Green
                }
            }
            else
            {
                continue;
            }
        }
     
        #Remove All permissions other than Read or Similar
        $RoleDefinitionBindings = $RoleAssignment.RoleDefinitionBindings
        For($i=$RoleDefinitionBindings.Count-1; $i -ge 0; $i--)
        {
            $RoleDefBinding = $RoleAssignment.RoleDefinitionBindings[$i]       
     
            IF( ($RoleDefBinding.Name -eq "Read") -or ($RoleDefBinding.Name -eq "Restricted Read") -or ($RoleDefBinding.Name -eq "View Only") -or ($RoleDefBinding.Name -eq "Limited Access") )
            {
                continue;
            }
            Else
            {
                $RoleAssignment.RoleDefinitionBindings.Remove($RoleAssignment.RoleDefinitionBindings[$i])
                $RoleAssignment.Update()
                Write-host  Removed  $RoleDefBinding.Name Permissions from $RoleAssignment.Member.Name -ForegroundColor Red
            }
        }
    }
    
    

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.