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

What is versioning in SharePoint Online?

The 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 document’s previous versions.

How to Enable Version History in SharePoint Online Document Library?

To enable versioning in SharePoint Online Document Library, do the following:

  1. Navigate to your document library >> Click on Settings gear >> and then the “Library Settings” Menu item.
  2. Under the Library settings page, click on “Versioning Settings”.
  3. Select the “Create major versions” option and enter the 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
  4. Scroll to the bottom of the page. Click “OK” to apply versioning setting changes.

Once versioning has been enabled, you can view the history of changes by selecting the document and clicking on the “Version History” option from the context menu or toolbar. This will display a list of all the previous versions, along with who made the changes and when they were made. You can then restore any previous version by selecting it from the list and clicking on the “Restore” button.

SharePoint Online: PowerShell to Enable Versioning in Document Library

Versioning is a critical feature of SharePoint Online, and it’s one that can be easily enabled through PowerShell. Versioning allows you to keep track of changes to your documents, and it also provides a way to restore previous versions if necessary. Enabling versioning is a quick and easy way to ensure that your documents are always well-protected. To enable versioning for a document library, simply use the following PowerShell script.

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 the PnP PowerShell cmdlet Set-PnPList as well:

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

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

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

This will enable versioning for the given document library. 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 - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

Leave a Reply

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