Set SharePoint Online Site to Read Only using PowerShell

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

How to make a SharePoint Online site read only?

During a migrated project, we wanted to set a SharePoint Online site collection to read-only in order to enforce Data consistency. To make SharePoint Online site collection read-only, use this PowerShell script in 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:

#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 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 -UseWebLogin

    #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
 } 

Here is my another post on making site read only using site policy: SharePoint Online: Make Site Collection Read only using Site Policies?

Salaudeen Rajack

Information Technology Professional with Two decades of SharePoint Experience.

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

  • September 20, 2020 at 1:43 PM

    Thank you very much! This saved my bread.

    Reply

Leave a Reply