SharePoint On-premises – PowerShell script to unlock all site collections in Web Application. Required in SharePoint migration

PowerShell script to unlock site collections from web application
PowerShell script to unlock site collections from web application

Hi All,

Greetings for the day 🙂 LIFE IS BEAUTIFUL 🙂

SharePoint migration continues. Today sharing one more small PowerShell script. PowerShell script to unlock all site collections in given web application

Background :

  • We are migrating SharePoint application from Moss to SharePoint 2016
  • We are using content database attach approach
  • We have one web application having 500+ site collections, and 10 databases
  • So we started with first step – we have locked all site collections in Moss / SharePoint 2007 environment so that those should not get updated by the users
  • So next we followed below steps :
    • Created empty web application in SharePoint 2010 environment
    • Deployed our custom solutions on new web application in SharePoint 2010 environment
    • Took the backup of databases from MOSS DB server
    • Restored the databases on new SharePoint 2010 database server
    • Tested the restored databases on SharePoint 2010 database server against new web application in SharePoint 2010 environment – used the command Test-SPContentDatabase
    • Fixed few minor issues and started mounting the respective restored databases to new web application in SharePoint 2010 environment – used the command Mount-SPContentDatabase
    • After successful mounting the databases, next step is visual upgrade. When we started visual upgrade using updating UIVersion property of web (SPWeb) to 4
    • While doing visual upgrade we are getting following exception : Get-SPWeb : Access to this Web site has been blocked. Please contact the administrator to resolve this problem.
Get-SPWeb : Access to this Web site has been blocked. Please contact the administrator to resolve this problem
Fig : Error – Access to this Web site has been blocked. Please contact the administrator to resolve this problem
  • We found that all site collections are migrated in locked state
  • So we need to unlock those and hence requirement born 🙂

Following are the step by step PowerShell script to unlock all site collections in given web applications :

#Add Microsoft SharePoint snap-in to the current session of PowerShell  
Add-PsSnapin Microsoft.SharePoint.PowerShell

  • Use Start-SPAssignment CMDLET :
    • Large amounts of memory are required when we use objects like SPWeb, SPSite, or SPSiteAdminsitration
    • So the use of these objects, or lists of these objects, in Windows PowerShell scripts requires memory management
    • By default, all Get commands dispose of these objects immediately after the pipeline finishes, but by using SPAssignment, we can assign the list of objects to a variable and dispose of the objects after they are no longer needed
    • You can also ensure that the objects remain as long as you need them, even throughout multiple iterations of commands
#The Start-SPAssignment cmdlet properly disposes of objects used with variable #assignments
Start-SPAssignment -Global
#get the web application of which we need to visually upgrade the sites
$webApp = Get-SPWebApplication "<My WebApplication>"

  • Get all sites from the respective web application
#Get all sites
$sites = $webApp.Sites

  • Loop through each site collection and using LockState property of site collection (SPSite) object, unlock the site collections
foreach($spsite in $sites)
{
   #Set-SPSite -Identity "<sitecollection URL>" -LockState "<State>"

   #Where:
    # <sitecollection URL> - URL of site collection that we want to lock or    
                             unlock
    # <State> is one of the following :

      # Unlock => unlock the site collection and make it available to users
      # NoAdditions => prevent users from adding new content to site  
                       collection. Updates and deletions are still allowed
      # ReadOnly => prevent users from adding, updating, or deleting content
      # NoAccess => prevent users from accessing site collection and its 
                    content. Users who attempt to access the site receive an  
                    above error message
           
      # update the lock state => Unlock, NoAdditions, 
    	Set-SPSite -Identity $spsite -LockState "Unlock"

      # maintain the the logs
        Write-Host "Site unlocked - " $spsite.Url >> "D:\\PS\\migration 
        changes\\PowerShellScripts\\unlocksitelogs.txt"
        Write-Output "Site unlocked - " $spSite.Url >> "D:\\PS\\migration 
        changes\\PowerShellScripts\\unlocksitelogs.txt"
        write-output " "
}#foreach

  • Finally use the CMDLET – Stop-SPAssignment
Stop-SPAssignment
    [[-SemiGlobal] <SPAssignmentCollection>]
    [-AssignmentCollection <SPAssignmentCollection>]
    [-Global]
    [<CommonParameters>]

Stop-SPAssignment -global

Complete Script :

#Add Microsoft SharePoint snap-in to the current session of PowerShell  
Add-PsSnapin Microsoft.SharePoint.PowerShell

#The Start-SPAssignment cmdlet properly disposes of objects used with variable #assignments
Start-SPAssignment -Global

#get the web application of which we need to visually upgrade the sites
$webApp = Get-SPWebApplication "<My WebApplication>"

foreach($spsite in $sites)
{
   #Set-SPSite -Identity "<sitecollection URL>" -LockState "<State>"

   #Where:
    # <sitecollection URL> - URL of site collection that we want to lock or    
                             unlock
    # <State> is one of the following :

      # Unlock => unlock the site collection and make it available to users
      # NoAdditions => prevent users from adding new content to site  
                       collection. Updates and deletions are still allowed
      # ReadOnly => prevent users from adding, updating, or deleting content
      # NoAccess => prevent users from accessing site collection and its 
                    content. Users who attempt to access the site receive an  
                    above error message
           
      # update the lock state => Unlock, NoAdditions, 
    	Set-SPSite -Identity $spsite -LockState "Unlock"

      # maintain the the logs
        Write-Host "Site unlocked - " $spsite.Url >> "D:\\PS\\migration 
        changes\\PowerShellScripts\\unlocksitelogs.txt"
        Write-Output "Site unlocked - " $spSite.Url >> "D:\\PS\\migration 
        changes\\PowerShellScripts\\unlocksitelogs.txt"
        write-output " "
}#foreach

Stop-SPAssignment -global

Thanks for reading 🙂 If its worth at least reading once, kindly please like and share 🙂 SHARING IS CARING 🙂

Enjoy the beautiful life 🙂 Have a FUN 🙂 HAVE A SAFE LIFE 🙂 TAKE CARE 🙂

Prasham Sabadra

LIFE IS VERY BEAUTIFUL :) ENJOY THE WHOLE JOURNEY :) Founder of Knowledge Junction and live-beautiful-life.com, Author, Learner, Passionate Techie, avid reader. Certified Professional Workshop Facilitator / Public Speaker. Scrum Foundation Professional certificated. Motivational, Behavioral , Technical speaker. Speaks in various events including SharePoint Saturdays, Boot camps, Collages / Schools, local chapter. Can reach me for Microsoft 365, Azure, DevOps, SharePoint, Teams, Power Platform, JavaScript.

You may also like...

1 Response

  1. July 1, 2021

    […] We have an article for PowerShell script to change the lock state of Site or all sites in given web application, please have a look – SharePoint On-premises – PowerShell script to unlock all site collections in Web Application […]

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: