11 November 2011

Lock or Unlock all Site Collection in a Web Application

For many reason, like a maintenance window on a SharePoint Farm, you might want to lock all the site collection from a Web Application

I have created an automated script that will do that for you.


param (
    [string]$WebAppUrl = $(throw "Missing WebApp URL (please use -webappurl [URL])"),
    [string]$LockState= $(throw "Missing LockState (please use -lockstate [Unlock/ReadOnly])")

#Addin SharePoint2010 PowerShell Snapin
Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue

$WebApp = Get-SPWebApplication $WebAppUrl
$AllSites = $WebApp | Get-SPSite

#Set each SiteCollection with the lockstate defined
foreach ($Site in $AllSites)
    Write-Host "Setting " -nonewline
    Write-Host $Site.url -ForegroundColor Yellow -nonewline
    Write-Host " to $lockState..." -nonewline
    Set-SPSiteAdministration -LockState $lockState -Identity $Site.url
    Write-Host "[OK]" -foregroundcolor green

Paste the above code in a PowerShellScript file, like [SPSiteCollectionLock.ps1] and copy it on your local SharePoint Server Drive.

Launch the script from a Windows Powershell Cmd Prompt using the following parametters:
.\SPSiteCollectionLock.ps1 -webappurl <URL> -lockstate [Unlock/ReadOnly]

Et voila !