SharePoint Online: How to Lock a Site Collection using PowerShell?

How to Lock a SharePoint Online Site Collection?

In SharePoint Online, you can lock a site collection so that no one can access it. In real-time, you may want to temporarily freeze a site while it is in development or undergoing some maintenance activity. This article will show you how to lock a site collection using PowerShell.

Lock SharePoint Online Site with PowerShell:

Set the variables and run this PowerShell script to lock a site collection in SharePoint Online using the Set-SPOSite cmdlet.

#Variables for Admin Center and Site Collection URL
$AdminCenterURL = ""
$SiteURL =""

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

Set-SPOSite -Identity $SiteURL -LockState "NoAccess" 

So when a user tries to access the locked site, they will receive a 403 error.

how to lock sharepoint online site collection

SharePoint Online: PowerShell to Get Lock Status

You can verify the lock status of a Site collection with the following script:

Get-SPOSite -Identity $SiteURL | select Title,URL,LockState

Similarly, PnP PowerShell to get the lock state of a site goes as:

$TenantAdminURL = ""
$SiteURL = ""
Try {
    #Connect to Admin Center
    Connect-PnPOnline -Url $TenantAdminURL -Interactive
    #Get Lock Status of the site
    Get-PnPTenantSite -Identity $SiteURL | Select URL, LockState
Catch {
    Write-Host -f Red "Error:"$_.Exception.Message

To unlock a SharePoint Online Site Collection:

How to unlock a SharePoint Online site? Just pass the value “Unlock” to the “LockState” parameter!

Set-SPOSite -Identity $SiteURL -LockState "Unlock"

This will unlock the site that we just locked in the previous command.

Optionally, You can redirect users to another page and provide them with some useful information, such as “Site is Locked. Contact Support”, etc. So, that when a user tries to access the locked site collection, they will be redirected to the URL you provided.  This needs to be applied at the tenant level.

Set-SPOTenant -NoAccessRedirectUrl ""

Here, the “NoAccessRedirectUrl” refers to the URL redirecting a user when a site has been locked. Please note, the “NoAccessRedirectUrl” setting takes some time to reflect!

sharepoint online lock site collection
You can use this trick to lock OneDrive site collection also. Just set the site collection URL to: and run the script!

How to Lock a SharePoint Online site using PnP PowerShell?

We can lock or unlock site collections with PnP PowerShell as well.

$TenantURL = ""
$SiteURL = "" 

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

    #Set Site to Read-only
    Set-PnPTenantSite -Url $SiteURL -LockState NoAccess
    Write-Host "Site is Locked Successfully!" -f Green  
Catch {
    Write-Host -f Red "Error:"$_.Exception.Message

It takes a while, and as soon as it’s locked, you’ll see a “403 FORBIDDEN” message on locked sites! If you want to make a SharePoint Online site collection read-only, use: How to Make a SharePoint Online Site Collection Read Only?

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!

Leave a Reply

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