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.
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)'"
}
The same thing can be done with PnP PowerShell, as:
$SiteURL = "https://crescent.sharepoint.com/sites/Funds"
$LibraryName = "Reports"
#Connect to SharePoint Online site
Connect-PnPOnline $SiteURL -Interactive
#Get the Library
$Library = Get-PnPList -Identity $LibraryName
#Set Version History Limit
Set-PnPList -Identity $Library -MajorVersions 5
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
}
In conclusion, Setting up versioning limits in SharePoint Online can help to control the amount of storage used by older versions of documents, and keep your environment running smoothly. By using PowerShell, you can easily configure versioning limits for specific libraries or lists, giving you more control over your SharePoint environment. By following the steps outlined in this guide, you should now be able to set versioning limits in SharePoint Online using PowerShell. It is important to consider the needs of your organization when setting versioning limits, such as how many versions are needed for compliance or how much storage space is available. Additionally, it is a good practice to review the versioning limit settings regularly to ensure that they are still meeting the needs of your organization.
Is there any way to set versioning history to the tenant level (ex. contoso.sharepoint.com) trough powershell?
i would like to apply to all collections sites, subsites and all document library contained in them
My organization doesn’t permit connecting to our SPO server by PowerShell (and it’s unlikely I can get this changed). Is there a way to adjust the versioning limit via Web Services / REST API?
Is it true that SharePoint Online stores a complete version of each file that is modified, even if only metadata is modified? If so, this differs from SP Server which stored only the delta. This has huge ramifications for how versions should be managed in SPO.
Shocking isn’t it. From what I’ve read (and I’ve searched far and wide) SPO is completely oblivious to SQL Shredded Storage. Each Version is a full copy, and this full copy counts towards your storage entitlement. “Historically, versioning is not enabled by default at the creation of a list or library. Recently, SharePoint Online has started enabling it by default in libraries when they’re created” and setting retention to 500 major versions… cha-ching..
🙁
Hi Salaudeen,
Is there a way to limit the version history to only 1?
Versions are taking up 70% of our storage. We make lots of changes to be very large 3D files.
I have local backup for version history.
I have no Powershell experience.
Thanks,
Tim
You can’t set it through the web browser interface, but PowerShell can help! You can turn-off the version history as well: How to Turn OFF Versioning in SharePoint Online?
Hello Salaudeen,
If I understood corretly, we dont have control over the dafult 500 value. What do you recommend for the new site creations? Restrict the users from creation in first step and then instruct the admins to modify the versioning setting for each site creation? or maybe, modifing the script to identify the new creation sites on a daily/weekly bases and run it via Schedule task?
Cheers,
Hatef
Unfortunately, that’s the only solution! User education and scheduled script will help addressing this issue.
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.
Just loop through all sites in the tenant and call the function Set-PnPVersionHistoryLimit SharePoint Online: PowerShell to Iterate through All Site Collections
i’m struggling to understand where to insert this, could you provide some more details if possible, i need to change all sites in the collection version history to the minimum of 100 as we’re having huge storage issues with pptx files having 500 versions and being 100’s gb. Thanks!
Great script. Remember to run `Register-PnPManagementShellAccess` first so you can use -Interactive in your Connect-PnPOnline with MFA
Of course! As explained in: How to Install PnP PowerShell for SharePoint Online?
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
Hello
is there a possibility with powershell to limit the history with the timestamp and not with the number of versions?
thank you
Very thank you very much for all these scripts. However, I thought this script would also handle the library of sites and sub-sites.
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?
50000 is the hard limit set by Microsoft.
is there script to export current versioning settings for each library in onedrive?
Here you go: SharePoint Online: Versioning Analysis on All Lists and Libraries using PowerShell, Just change the $SiteURL parameter to your OneDrive site URL.
What is the trigger that creates a ‘version’? Is that different for online apps vs desktop apps?
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
Well, This happens when the list items > 5000! Script has been updated to handle the view threshold.
Hi thank you for this script, if I apply a limit of 5 versions will it remove all the other versions?
Thanks
Eric
Hi Eric,
Unfortunately, No! (Until and Unless you edit and save the file!). To cleanup previous versions, You can use this PowerShell: PowerShell to Clean-up Version History in SharePoint Online
Ah great thank you