SharePoint Online: Enable Versioning on All Lists and Libraries using PowerShell

Requirement: Enable Versioning on all lists in a SharePoint Online site.

How to Enable Versioning on a List or Library in SharePoint?

The Version History feature in SharePoint allows you to access a previously saved copy of a document or list item. By default, versioning is enabled on your document library in SharePoint Online. When a save operation happens, SharePoint creates a new version along with metadata such as, who saved the item, when it was created, etc. To enable versioning for a list or library in SharePoint Online, follow these steps:

  1. Go to your document library >> Click the Library tab on the Ribbon >> Click on Library Settings.
  2. Under General Settings, click the Versioning Settings link.
  3. From the Versioning Settings page, choose “Create major versions”. Optionally, you can set a limit on the number of versions.
    sharepoint online enable version history
  4. This enables SharePoint Online document library versioning. Once the versioning feature is enabled, you can select a document or a list item and click on “Version History” from the context menu or from the Ribbon to view versions created.

SharePoint Online: PowerShell to Enable Versioning

Do you need to enable versioning for a document library in SharePoint Online? PowerShell can help! Here is the PowerShell to enable version history on a given list or library:

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

Function Enable-SPOVersioning()
{
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName
    )
    Try {
        #Setup Credentials to connect
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
         
        #Get the List
        $List = $Ctx.Web.Lists.GetByTitle($ListName)
        
        #sharepoint online powershell enable versioning
        $List.EnableVersioning = $True
        $List.MajorVersionLimit = 50
        $List.Update()
        $Ctx.ExecuteQuery() 
        Write-host -f Green "Versioning has been turned ON at $ListName"
 
    }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
}

#Set Parameters
$SiteURL="https://Crescent.sharepoint.com"
$ListName="Projects"

#Call the function 
Enable-SPOVersioning -SiteURL $SiteURL -ListName $ListName

PowerShell script to enable versioning on all lists and libraries in SharePoint Online:

If you’re like me, you’re always looking for ways to automate tasks and save time. With that in mind, I wanted to share a quick tip on enabling versioning for all lists and libraries in a SharePoint Online site using PowerShell. Let’s activate version history on all lists and libraries of a SharePoint Online site:

#Load SharePoint Online Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
   
##Variables for Processing
$SiteUrl = "https://portal.crescent.com.com/sites/Sales/"
$UserName="Salaudeen@crescent.com"
$Password ="Password goes here"

try{  
    #Setup the context
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Context.Credentials = $credentials
    
    #get the site and lists
    $web = $context.Web
    $context.Load($web)
    $context.load($web.lists)
    $Context.executeQuery() 
 
    #Iterate through each list in the web
    foreach($list in $web.lists){
    if ($list.hidden -eq $false)
    {
        #Enable versioning
        $list.EnableVersioning = $true
        $lIST.MajorVersionLimit = 50
        $list.Update()
        $Context.ExecuteQuery() 
        Write-host "Versioning has been turned ON at :"$List.title
    }
  }
}
catch{
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
} 

This script turns on versioning in SharePoint Online for all lists and libraries on the given site. If you want to filter and enable versioning on document libraries alone, use the following:

If($List.BaseType -eq "DocumentLibrary")
{
   #Enable version history
}

SharePoint Online: Enable Versioning using PnP PowerShell

To check if the versioning feature is enabled for the List, use: $List.EnableVersioning

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName ="Projects"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get List
$List = Get-PnPList $ListName 

If($List)
{ 
    #sharepoint online enable versioning powershell
    Set-PnPList -Identity $ListName -EnableVersioning $True -MajorVersions 20
}

Similarly, you can configure versioning settings for all lists on a site

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get All Lists from the web
$Lists = Get-PnPList | Where {$_.Hidden -eq $false}
ForEach($List in $Lists)
{ 
    #Enable versioning and set Number of versions to 50
    Set-PnPList -Identity $List -EnableVersioning $True -MajorVersions 50
    Write-host -f Yellow "Configured Versioning on List:"$List.Title
}

Likewise, To enable minor versions, use:

Set-PnPList -Identity $ListName -EnableVersioning $True -EnableMinorVersions $True -MajorVersions 25 -MinorVersions 25

Enable Versioning with Major and Minor versions for All Document Libraries in a site

Here is the PnP PowerShell to enable minor versions for all libraries:

#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/Neo"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Get all document libraries
$Libraries =  Get-PnPList | Where {$_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $False}

ForEach($Library in $Libraries)
{ 
    #sharepoint online enable versioning powershell
    Set-PnPList -Identity $Library -EnableVersioning $True -EnableMinorVersions $True
}

How about setting the version history configuration on all document libraries for the entire site collection?

#Config Variables
$SiteURL = "https://crescent.sharepoint.com/Sites/Retail"

#Exclude certain libraries
$ExcludedLists = ("Preservation Hold Library","Site Assets","Site Pages","Style Library","Form Templates")

Function Enable-PnPVersionHistory($SiteURL)
{
    
    Write-host "Processing Web $($_.URL)" -f Cyan
    Connect-PnPOnline -Url $SiteURL -Interactive

    #Get All Document Libraries
    $Lists = Get-PnPList | Where-Object {$_.Hidden -eq $False -and $_.BaseType -eq "DocumentLibrary" -and $ExcludedLists -notcontains $_.Title}

    #Enable Version History for Document Libraries
        $Lists | ForEach-Object {
        $Params = @{
            "Identity"         = $_.Title
            "EnableVersioning" = $True
            "MajorVersions"    = 50
            "MinorVersions"    = 10
        }
        #Set-PnPList @Params
        Write-host "Enabled Version History for "$_.Title -f Green
    }
    
}

#Connect to SharePoint Online Site from PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Get all Webs and call the function to set version history
Get-PnPSubWeb -IncludeRootWeb | ForEach-Object { Enable-PnPVersionHistory $_.URL }
How long does SharePoint Online keep version history? Forever!

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

6 thoughts on “SharePoint Online: Enable Versioning on All Lists and Libraries using PowerShell

  • Any reference to a similar PowerShell to enable versioning on all libraries that exist in Subsites of a Site Collection to avoid having to do each site 1 by 1? Thanks!

    Reply
    • Post has been updated with a script to update version history on all libraries in a site collection – including all from subsites.

      Reply
  • how can I use either of these scripts to turn on versioning on ALL lists/libraries across ALL site collections and subsites?

    Reply
  • How to check that which doc libs has versioning enabled or disabled?

    Reply
  • Amazing Article and indeed very very helpful in disabling the versioning of the DLs and sites. I am interested in knowing how can we disable the same for the new sites that are being created. This script would work only on the current sites.

    Did someone try that

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *