When maintenance or updates are complete, or if you've mistakenly locked site collections, you'll need to unlock them.

Here’s how you can use PowerShell to unlock all site collections within a SharePoint 2019 on-premises web application efficiently.

Prerequisites

  • Environment: The script should be run in the SharePoint Management Shell.
  • Permissions: You must have administrative rights to manage site collections.

Script Configuration
Configure the following variables before executing the script:

  • $webAppUrl: Enter the URL of your SharePoint web application (e.g., http://your-webapp-url).
  • $outputFile: Specify the path for saving the CSV report (e.g., C:\UnlockedSitesReport.csv).

How the Script Functions

  1. Load SharePoint Snap-In: Ensures the SharePoint PowerShell snap-in is available.
  2. Define Variables: Sets up the web application URL and the path for the CSV output.
  3. Initialize Report Array: Prepares $siteStatusList for storing unlock status information.
  4. Loop Through Site Collections:
    • For each site:
      • Checks if the site is already unlocked (-not ($site.ReadOnly -or $site.WriteLocked)).
      • If locked, it removes the restrictions by setting both ReadOnly and WriteLocked to false.
      • Records the site's URL, unlock status, and the timestamp.
  5. Export to CSV: Generates a report of the unlock actions taken.

CSV Report Details

  • URL: URL of each site collection.
  • LockStatus: Whether the site was "Already Unlocked" or "Unlocked" during script execution.
  • TimeUnlocked: The time when the unlock occurred or was verified.

The Script

# Load SharePoint PowerShell snap-in if not loaded
if ((Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null) {
    Add-PSSnapin Microsoft.SharePoint.PowerShell
}

# Define the URL of your web application
$webAppUrl = "http://your-webapp-url"
# Define the output CSV file path
$outputFile = "C:\UnlockedSitesReport.csv"

# Initialize an array to store site information
$siteStatusList = @()

# Get the Web Application object
$webApp = Get-SPWebApplication $webAppUrl

# Iterate through each site collection in the web application
foreach ($site in $webApp.Sites) {
    try {
        # Check if the site is already unlocked (not ReadOnly and not WriteLocked)
        $isAlreadyUnlocked = -not ($site.ReadOnly -or $site.WriteLocked)
        $lockStatus = if ($isAlreadyUnlocked) { "Already Unlocked" } else { "Unlocked" }
        
        # If not already unlocked, remove the lock by setting ReadOnly and WriteLocked to false
        if (!$isAlreadyUnlocked) {
            $site.ReadOnly = $false
            $site.WriteLocked = $false
            $site.Update()
        }

        # Collect site information for the CSV report
        $siteStatusList += [PSCustomObject]@{
            URL           = $site.Url
            LockStatus    = $lockStatus
            TimeUnlocked  = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
        }

        Write-Host "$lockStatus for site collection:" $site.Url -ForegroundColor Green
    }
    catch {
        Write-Host "Failed to process site collection:" $site.Url -ForegroundColor Red
    }
    finally {
        # Dispose of the site object to free up resources
        $site.Dispose()
    }
}

# Export the site status list to a CSV file
$siteStatusList | Export-Csv -Path $outputFile -NoTypeInformation -Encoding UTF8

Write-Host "All site collections in the web application have been processed. Report saved to $outputFile" -ForegroundColor Cyan

Additional Resources
For more scripts and tools tailored for SharePoint management, visit my GitHub repository at: MuckyRat/SharePoint-Server-UnlockSites.

This script serves as an essential tool for SharePoint administrators needing to restore access to their site collections after a period of lock-down, ensuring users can resume their work without delay.

Remember, always test scripts in a safe environment before applying them in production.