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.

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

$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.

Optionally, You can redirect users to another page and provide them 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 "https://crescent.sharepoint.com/SitePages/Site-locked.aspx"

Here, the “NoAccessRedirectUrl” refers to the URL to redirect 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 - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

Leave a Reply

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