I am trying to figure out how to create a script to set all the sites with lockstate "ReadOnly" to lockstate "NoAccess" without going through each site individually and changing the lockstate.

I have so far figured out how to get a list of all SharePoint sites in the collection with Get-SPOSite, how to check their lockstate and how to change it but I don't know where to start with writing the script to automate this process.

1 Answer 1


Please run the following PowerShell script as an administrator:

Import-Module Microsoft.Online.SharePoint.Powershell -DisableNameChecking
#Config Parameters

#Get Credentials to connect to SharePoint Admin Center
$Cred = Get-Credential
#Connect to SharePoint Online Admin Center
Connect-SPOService -Url $AdminSiteURL –Credential $Cred
#Get All site collections
$SiteCollections = Get-SPOSite -Limit All
Write-Host "Total Number of Site collections Found:"$SiteCollections.count -f Yellow
#Array to store Result
$ResultSet = @()
#Loop through each site collection and retrieve details
Foreach ($Site in $SiteCollections)
    Write-Host "Processing Site Collection :"$Site.URL -f Yellow
    if($Site.LockState -eq 'ReadOnly'){

    #Get site collection details   
    $Result = new-object PSObject
    $Result | add-member -membertype NoteProperty -name "Title" -Value $Site.Title
    $Result | add-member -membertype NoteProperty -name "Url" -Value $Site.Url
    $Result | add-member -membertype NoteProperty -name "LockState" -Value $Site.LockState
    $ResultSet += $Result


#List Result
Foreach ($Result in $ResultSet)
    set-sposite $Result.Url -LockState NoAccess

enter image description here

enter image description here

  • Yup, that worked a perfectly, thanks very much!
    – Bosco
    Feb 10, 2021 at 12:03
  • Thanks for your reply. I am glad this can help you. Have a nice day! Feb 11, 2021 at 1:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.