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. However, there may be times when you need to increase or decrease the limit on the number of versions that are stored for a given document library. In this article, we will walk you through the steps needed to set the versioning limit for a document library in SharePoint Online.

To set a limit on the number of versions for a library in SharePoint Online, do the following:

  • 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

Using PowerShell, let’s apply the limit on version history for a particular document library:

#Import SharePoint Online PowerShell Module
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
 
#Set Parameters
$SiteURL="https://Crescent.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)'"
}

This can be helpful if you want to keep your site’s file size down, or if you just don’t need or want to retain too many older versions of documents. How about all document libraries in a SharePoint Online site? Let us add some error handling, 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://Crescent.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 the 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
        Connect-PnPOnline -Url $Web.URL -Interactive

        #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 -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 -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 -Interactive 

#Get webs of the Site Collection
$Webs = Get-PnPSubWeb -Recurse -IncludeRootWeb
 
ForEach($Web in $Webs)
{ 
    Set-PnPVersionHistoryLimit -Web $Web
}

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!

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

  • Is there a way for this too loop through all sites in the tenant? We currently have a major issue with version history taking up over 50% of our sharepoint space allowance and its due to all sites have 500 version limits, and pptx file becoming huge. We’ve used Power Automate to remove all the old versions, but now need to make sure every sites version limit is maximum 100.

    Reply
  • Great script. Remember to run `Register-PnPManagementShellAccess` first so you can use -Interactive in your Connect-PnPOnline with MFA

    Reply
  • Make sure you have the latest version of PnP.PowerShell, as many things have changed:
    Uninstall-Module -Name SharePointPnPPowerShellOnline -AllVersions -Force
    Install-Module -Name PnP.PowerShell

    Reply
  • Hello

    is there a possibility with powershell to limit the history with the timestamp and not with the number of versions?

    thank you

    Reply
  • Very thank you very much for all these scripts. However, I thought this script would also handle the library of sites and sub-sites.

    Reply
  • hi i wanna ask you. how about make all the sharepoint from administrator-view to be 99999 versioning?
    so all the employees from many branchs wont stuck in 500.
    thanks. and what should i change from powershell’s script?

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

    Reply
    • Here you go: SharePoint Online: Versioning Analysis on All Lists and Libraries using PowerShell, Just change the $SiteURL parameter to your OneDrive site URL.

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

    Reply
  • 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
    • Well, This happens when the list items > 5000! Script has been updated to handle the view threshold.

      Reply
  • 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