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 it is undergoing some sort of maintenance activity.

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 & 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
You can verify the lock status of a Site collection with:
Get-SPOSite -Identity $SiteURL | select Title,URL,LockState

To unlock a SharePoint Online Site Collection:
How to unlock a SharePoint Online site? Just pass the value "Unlock" to "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 ""
Here the "NoAccessRedirectUrl" refers URL to redirect a user when a site has been locked. Please note the "NoAccessRedirectUrl" setting takes sometime 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 -UseWebLogin

    #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 its locked, you'll see "403 FORBIDDEN" message on locked sites!

If you want to make SharePoint Online site collection read only, use: How to Make a SharePoint Online Site Collection Read Only?

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.