SharePoint Online: How to Enable Versioning on a Document Library using PowerShell?

What is versioning in SharePoint Online?

Version history feature in SharePoint Online tracks every change to the document and creates a copy of it when someone changes a document. Once you enable versioning settings for a List or Library, Versioning features are turned ON and all changes will be tracked. Site owners can view, manage, and restore previous versions of items. Each version of the document has the following:

  • Version No
  • When the version was created
  • Who made the version (change)
  • Size of the version

Once the versioning feature is enabled, you can view/restore the previous versions of the document.

How to Enable Version History in SharePoint Online Document Library?

To Enable or Disable Versioning in SharePoint Online Document Library:

  • Navigate to your document library >> Click on Settings gear >> and then “Library Settings” Menu item.
  • Under Library settings page, Click on “Versioning Settings”
  • Select “Create major versions” option and enter number of versions to track (by default, this is enabled with 500 versions in SharePoint Online). sharepoint online powershell to enable versioning in document library
  • Scroll to the bottom of the page. Click “OK” to apply versioning setting changes.

SharePoint Online: PowerShell to Enable Versioning in Document Library

Here is how to enable versioning in SharePoint Online using PowerShell:

#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-DocLibraryVersioning()
{
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $DocLibraryName
    )    
    Try {
    #Setup Credentials to connect
    $Cred = Get-Credential
    $Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Cred

    #Get the Library from the web
    $Library = $Ctx.Web.Lists.GetByTitle($DocLibraryName)

    #enable versioning in sharepoint online
    $Library.EnableVersioning = $True
    $Library.MajorVersionLimit = 50

    $Library.update()
    $Ctx.ExecuteQuery()
    
    write-host  -f Green "Version History Enabled for Document Library!"
    }
    Catch {
        write-host -f Red "Error enabling versioning in Document Library!" $_.Exception.Message
    }
}

#Set Parameters
$SiteURL= "https://crescent.sharepoint.com/"
$DocLibraryName="Project Docs"

#Call the function to enable versioning in document library
Enable-DocLibraryVersioning -siteURL $SiteURL -DocLibraryName $DocLibraryName

SharePoint Online: Enable Versioning using PnP PowerShell

We can enable versioning with this PnP PowerShell as well:

#Set Parameters
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
$LibraryName = "Documents"

#Connect to PnP Online to the Site
Connect-PnPOnline -Url $SiteURL -UseWebLogin

#Enable Versioning
Set-PnPList -Identity $LibraryName -EnableVersioning $True

To Enable Minor Versions, use:

Set-PnPList -Identity $LibraryName -EnableVersioning $True -EnableMinorVersions $True

To Set Limits on Versions:

Set-PnPList -Identity "Documents" -EnableVersioning $True -EnableMinorVersions $True -MajorVersions 500 -MinorVersions 100

To Enable version history on all lists and libraries, use: SharePoint Online: Enable Versioning on All List and Libraries using PowerShell

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

Leave a Reply