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

Versioning in SharePoint Online is crucial to document management, as it allows you to track changes made to your files and restore previous versions if necessary. 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. Once versioning is enabled, SharePoint Online will automatically create a new version of a document or list item every time it is edited, and you can view and restore previous versions as needed.

To Enable Minor Versions, use the following script:

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

Wrapping up

We have discussed how to enable versioning in SharePoint Online in this article. You can enable versioning for SharePoint Online document libraries and lists by following the steps outlined in this guide. You can track changes made to your files and restore previous versions if necessary. This can be a useful tool for managing document and list item changes and ensuring the integrity of your SharePoint Online content.

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

How many document versions does SharePoint keep?

By default, SharePoint retains a minimum of 100 versions to a maximum of 50000 of a document! By changing the versioning settings, you can adjust the maximum number of document versions that SharePoint keeps.

How to get the previous version of the SharePoint document?

To get the previous version of a SharePoint document, Go to the document library and right-click on the document name, and then click “Version History” from the menu. You’ll see a list of versions of the document. From there, you can select the version you want to view. You can also restore the version by selecting “Restore”.

How to publish a major version in SharePoint?

To publish a major version in SharePoint, select the document and click “Check in” from the toolbar. From there, select “Major version (Publish)”. Alternatively, you can right-click on the file and select “More” and then “Major Version (Publish)”. Publishing a major version makes it available to other users and indicates that it is a stable version of the document.
More info: How to Publish a Major version of a document in SharePoint Online?

How to restore the previous version in SharePoint?

You can access the version history of a document or item by right-clicking on the document or item and selecting “Version History”. To restore a previous version in SharePoint, select the version you want to restore by clicking on its drop-down menu. From there, select “Restore” to restore the selected version.
More info: How to restore a previous version of a document in SharePoint?

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!

Leave a Reply

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