how to set version limit in all ducument libraries

christieA-9828 20 Reputation points
2024-09-05T07:21:56.5166667+00:00

In the SharePoint Online, how to set version limit in all document libraries. For example, I want to set maximum 50.

Thanks.

SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,938 questions
0 comments No comments
{count} votes

Accepted answer
  1. Emily Du-MSFT 45,746 Reputation points Microsoft Vendor
    2024-09-05T09:03:25.74+00:00

    You could use PowerShell to set version maximum limit as 50 in all document libraries.

    Note: Replace site URL with your own 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 = 50
        )
         
        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://tenant.sharepoint.com/sites/emilytest"
         
    #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
    }
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.