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

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.

- 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. Otherwise for SharePoint CMDLET we get an error as discussed in last article – PowerShell ISE – resolving issue – Get-SPWebApplication : The term ‘Get-SPWebApplication’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. Bit about Add-PSSnapin
#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 🙂
1 Response
[…] 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 […]
You must log in to post a comment.