SharePoint Online: Set Versioning Limit using PowerShell

Requirement: Set version history limit in SharePoint Online document library.

How to Configure Version History Limits in SharePoint Online?

By default, any document library created in SharePoint Online is enabled with a version history limit of 500. To limit the number of versions for a library in SharePoint Online,

  • Go to your document library >> Click on Settings >> Click on Library Settings.
  • Under General Settings, click the Versioning Settings link.
  • From the Versioning Settings page, choose “Create major versions” and for “Keep the following number of major versions” you can set the limit on the number of versions. sharepoint online version history limit

PowerShell to Set Version History Limit for a Document Library

Let’s apply limit on version history for a particular document library using PowerShell.

#Import SharePoint Online PowerShell Module
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
 
#Set Parameters
$SiteURL="https://crescentIntranet.sharepoint.com"
$ListName="Documents"
$VersioningLimit = 100

#Get Credentials to connect
$Cred= Get-Credential
  
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
          
#Get the List - Versioning settings
$List = $Ctx.Web.Lists.GetByTitle($ListName)
$List.Retrieve("EnableVersioning")
$Ctx.ExecuteQuery()
         
#Set version history limit
If($List.EnableVersioning)
{
    $List.MajorVersionLimit = $VersioningLimit
    $List.Update()
    $Ctx.ExecuteQuery()
    Write-host -f Green "Version History Settings has been Updated on '$($ListName)'"
}
Else
{
    Write-host -f Yellow "Version History is turned-off on '$($ListName)'"
}

How about all document libraries in a SharePoint Online site? Well, lets add some error handling and wrap the above script into a function and make it re-usable.

#Import SharePoint Online PowerShell Module
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
 
Function Set-SPOSiteVersionHistoryLimit()
{
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [parameter(Mandatory=$false)][int]$VersioningLimit = 100
    )
    Try {
        #Get Credentials to connect
        $Cred= Get-Credential
  
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)        
        
        #Get All Lists from the Web
        $Lists = $Ctx.Web.Lists
        $Ctx.Load($Lists)
        $Ctx.ExecuteQuery()
         
        #Array to exclude system libraries
        $SystemLibraries = @("Form Templates", "Pages", "Preservation Hold Library","Site Assets", "Site Pages", "Images",
                            "Site Collection Documents", "Site Collection Images","Style Library")
         
        #Get All document libraries
        $DocumentLibraries = $Lists | Where {$_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $False -and $_.Title -notin $SystemLibraries}
        
        #Set Versioning Limits
        ForEach($Library in $DocumentLibraries)
        {
            #Get the Library's versioning settings
            $Library.Retrieve("EnableVersioning")
            $Ctx.ExecuteQuery()

            #powershell to set limit on version history
            If($Library.EnableVersioning)
            {
                #Set versioning limit
                $Library.MajorVersionLimit = $VersioningLimit
                $Library.Update()
                $Ctx.ExecuteQuery() 
                Write-host -f Green "Version History Settings has been Updated on '$($Library.Title)'"
            }
            Else
            {
                Write-host -f Yellow "Version History is turned-off at '$($Library.Title)'"
            }
        }
    }
    Catch {
        Write-host -f Red "Error:" $_.Exception.Message
    }
}
 
#Call the function to set version history limit
Set-SPOSiteVersionHistoryLimit -SiteURL "https://crescentIntranet.sharepoint.com"

This PowerShell script sets the major version limit for all document libraries to 100 (from an optional parameter).

SharePoint Online: PnP PowerShell to Set Versioning Limit on All Document Libraries in a Site Collection

This time, let’s configure limit on version history for all document libraries in a site collection.

#Function to Set versioning limit on all lists in a web
Function Set-PnPVersionHistoryLimit
{
    param
    (
        [Parameter(Mandatory=$true)] $Web,
        [parameter(Mandatory=$false)][int]$VersioningLimit = 100
    )
    
    Try {
        Write-host "Processing Web:"$Web.URL -f Yellow

        #Array to exclude system libraries
        $SystemLibraries = @("Form Templates", "Pages", "Preservation Hold Library","Site Assets", "Site Pages", "Images",
                            "Site Collection Documents", "Site Collection Images","Style Library")
        
        $Lists = Get-PnPList -Web $Web -Includes BaseType, Hidden, EnableVersioning
        #Get All document libraries
        $DocumentLibraries = $Lists | Where {$_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $False -and $_.Title -notin $SystemLibraries}
        
        #Set Versioning Limits
        ForEach($Library in $DocumentLibraries)
        {
            #powershell to set limit on version history
            If($Library.EnableVersioning)
            {
                #Set versioning limit
                Set-PnPList -Identity $Library -Web $Web -MajorVersions $VersioningLimit
                Write-host -f Green "`tVersion History Settings has been Updated on '$($Library.Title)'"
            }
            Else
            {
                Write-host -f Yellow "`tVersion History is turned-off at '$($Library.Title)'"
            }
        }
    }
    Catch {
        Write-host -f Red "Error:" $_.Exception.Message
    }
}
 
#Parameters
$SiteURL ="https://crescent.sharepoint.com/sites/marketing"
    
#Connect to PnP Online
Connect-PnPOnline -URL $SiteURL -UseWebLogin
  
#Get the Root Web
$RootWeb = Get-PnPWeb
 
#Call the function for Root web
Set-PnPVersionHistoryLimit -Web $RootWeb
 
#Get All Webs in the site collection and Iterate through
$Webs = Get-PnPSubWebs -Recurse
ForEach($Web in $Webs)
{ 
    Set-PnPVersionHistoryLimit -Web $Web
}

Salaudeen Rajack

Information Technology Professional with Two decades of SharePoint Experience.

7 thoughts on “SharePoint Online: Set Versioning Limit using PowerShell

  • June 24, 2021 at 11:27 AM

    is there script to export current versioning settings for each library in onedrive?

    Reply
  • June 11, 2021 at 6:11 AM

    What is the trigger that creates a ‘version’? Is that different for online apps vs desktop apps?

    Reply
  • July 10, 2020 at 4:50 PM

    Getting this error now

    Get-PnPListItem : The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator.

    Any idea where you change the list view threshold?

    Eric

    Reply
    • July 10, 2020 at 6:14 PM

      Well, This happens when the list items > 5000! Script has been updated to handle the view threshold.

      Reply
  • July 10, 2020 at 12:07 PM

    Hi thank you for this script, if I apply a limit of 5 versions will it remove all the other versions?

    Thanks

    Eric

    Reply

Leave a Reply