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 the SharePoint Online site collection to read-only to enforce Data consistency. To make SharePoint Online site collection to read only, use this PowerShell script in SharePoint Online Management Shell.
#Set Parameters
$SiteURL = ""

#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:
$TenantURL = ""
$SiteURL = "" 

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?

1 comment:

Please Login and comment to get your questions answered!

Powered by Blogger.