Friday, April 20, 2018

set Lock State (No Access, ready only, no additions, unlock) of all site collections in a SharePoint farm using powershell

Normally site lock functionality is available at site collection level only and not at farm level or even web application level, I came across a requirement where in I was asked to lock all site collections in farm. Since my farm has more than 20000 site collection it would have been a tedious task if I had to to it manually one by one.

-----------------------------------------------------------------------------------------------------------------------
Have created a Powershell script to set the all site collection in a farm to "NO Access" mode.

Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue;
Start-SPAssignment -Global;
$webapps= Get-SPWebApplication;
$LockState= "NoAccess"; 
Write-Host "Setting all web applications to $($LockState)..." -ForegroundColor Yellow;
foreach ($webapp in $webapps)
{
$sites= Get-SPSite -WebApplication $webapp -Limit All;
$sites | ForEach-Object{
Write-Host "Setting site collection $_.Url to $($LockState)..." -ForegroundColor Yellow;
        Set-SPSiteAdministration -LockState $LockState -Identity $_.url;}
}
Stop-SPAssignment -Global;
Write-Host "Finished!" -ForegroundColor Green;

------------------------------------------------------------------------------------------------------------------------

Below are the valid values for LockState variable 

  • Unlock to unlock the site collection and make it available to users.
  • NoAdditions to prevent users from adding new content to the site collection. Updates and deletions are still allowed.
  • ReadOnly to prevent users from adding, updating, or deleting content.
  • NoAccess to prevent users from accessing the site collection and its content. Users who attempt to access the site receive an error message.

Get Site collection count in a farm

Use the below script to get count of all site collections in a farm.
$AllWA = Get-SPWebapplication;
foreach($wa in $AllWA){$WA.Sites | Measure-Object | Format-List Count};