Set SharePoint Online Site to Read Only using PowerShell

Requirement: Set a SharePoint Online site to read only using PowerShell.

How to make a SharePoint Online site read only?

If you have a SharePoint Online site that you would like to make read-only, PowerShell makes it easy! This blog post will show you how to set a SharePoint Online site to read-only using PowerShell.

What are some reasons, why you would like to make sites read-only? Maybe you want to protect sensitive data from being edited or deleted by other people in your organization, or you aim to make your site read-only for compliance reasons. Although you can set the site permissions of all users to read-only, This doesn’t stop site collection administrators from altering the content. But putting the site to read-only prevents everyone, including site collection administrators, from editing content but still allows them access so that they can view the changes.

During a migrated project, we wanted to set a SharePoint Online site collection to read-only mode, to enforce Data consistency. To make the Microsoft SharePoint Online site collection read-only, use this PowerShell script in the SharePoint Online Management Shell.

#Set Parameters
$AdminCenterURL="https://crescent-admin.sharepoint.com"
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"

#Connect to SharePoint Online
Connect-SPOService -Url $AdminCenterURL -Credential (Get-Credential)

#PowerShell to set sharepoint online site to read only
Set-SPOSite -Identity $SiteURL -LockState ReadOnly

Once the execution of the above script is completed, you’ll find a message appearing on top of the site: “SharePoint sites are read-only right now while we do some maintenance. We apologize for the inconvenience.”

Set SharePoint Online Site to Read Only using PowerShell

To unlock the site collection, use the following:

#Unlock site from read only mode
Set-SPOSite -Identity $SiteURL -LockState Unlock

PnP PowerShell to Set a Site Collection to Read-Only Mode

Here is how to lock a SharePoint Online site collection with PnP PowerShell:

#Parameters
$TenantURL = "https://crescent-admin.sharepoint.com"
$SiteURL = "https://crescent.sharepoint.com/sites/volver" 

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $TenantURL -Interactive

    #Set Site to Read-only
    Set-PnPTenantSite -Url $SiteURL -LockState ReadOnly
    Write-Host "Site set to Read-Only Mode Successfully!" -f Green  
} 
Catch {
    Write-Host -f Red "Error:"$_.Exception.Message
 } 

To check the lock state of a site, use:

Get-PnPTenantSite -Identity $SiteURL | Select LockState

How about finding all sites that are in Read-Only access?

 Get-PnPTenantSite | Where {$_.LockState -eq "ReadOnly" -and $_.Template -ne "RedirectSite#0"}

Here is another method for making a site read-only using site policy – site closure: SharePoint Online: Make Site Collection Read only using Site Policies?

Wrapping up

In conclusion, setting a SharePoint Online site to read-only using PowerShell is quick and straightforward. By following the steps outlined in this article, you can use PowerShell to easily set the SharePoint Online site to read-only for all users. This is a useful tool for organizations that need to temporarily limit access to a site, or for those who want to ensure that users cannot make changes to a site’s content. The use of PowerShell also provides a convenient way to automate the process, making it easier to manage multiple sites at once.

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

3 thoughts on “Set SharePoint Online Site to Read Only using PowerShell

  • Thanks! PNP works well

    Reply
  • Thank you very much! This saved my bread.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *