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

Requirement: Enable Versioning on all lists in 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:

  • Go to your document library >> Click the Library tab on the Ribbon >> Click on Library Settings.
  • Under General Settings, click the Versioning Settings link.
  • 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
  • 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="[email protected]"
$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 in the given site. If you want to filter and enable versioning on document libraries alone, use:

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 long does SharePoint Online keep version history? Forever!

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

5 thoughts on “SharePoint Online: Enable Versioning on All List 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
  • 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.