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 = "https://crescent-admin.sharepoint.com"
$SiteURL ="https://crescent.sharepoint.com/sites/marketing"

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

$SiteURL="https://crescent.sharepoint.com/sites/marketing"
Get-SPOSite -Identity $SiteURL | select Title,URL,LockState

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

#Parameters
$TenantAdminURL = "https://crescent-admin.sharepoint.com"
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
 
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.

Redirect Locked Sites

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

Set-SPOTenant -NoAccessRedirectUrl "https://crescent.sharepoint.com/SitePages/Site-locked.aspx"

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: https://YourDomain-my.sharepoint.com/personal/usersite 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.

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

4 thoughts on “SharePoint Online: How to Lock a Site Collection using PowerShell?

  • Not sure if this is still being looked at but, with the redirect URL, am I right in assuming that it works on all SharePoint sites that have become inaccessible not necessarily just the ones that have been placed in a “Locked State”?

    Reply
      • Ah so if say there was another reason the Site couldn’t be accessed (other than lockstate), it would not redirect it to that page as well?

        Just wanting to make sure that users don’t just immediately attribute it to their site had been locked 🙂

        Reply

Leave a Reply

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