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 = "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
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

To unlock a SharePoint Online Site Collection:
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 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: https://YourDomain-my.sharepoint.com/personal/usersite and run the script!

If you want to make SharePoint Online site collection read only, use: How to Make a SharePoint Online Site Collection Read Only?
SharePoint Online: How to Lock a Site Collection using PowerShell? SharePoint Online: How to Lock a Site Collection using PowerShell? Reviewed by Salaudeen Rajack on August 17, 2017 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.