SharePoint Tutorials

How to Set SharePoint Version History Limit With PowerShell

Learn how to set the version history limit for your SharePoint site using PowerShell. Efficiently manage document revisions and optimize storage.

Introduction

SharePoint

SharePoint offers a valuable versioning feature that allows you to track and manage document revisions. By enabling versioning, you can keep a record of changes made to documents, making it easier to review and revert to previous versions if needed. In this article, we will explore how to set the version history limit for an entire SharePoint site using PowerShell. By following the steps provided, you can efficiently manage versioning across your SharePoint environment.

Understanding SharePoint Version History Limit

Before we dive into the details of setting the version history limit, let’s briefly discuss what it entails. Version history limit refers to the maximum number of versions retained for each document in SharePoint. By setting a limit, you control the number of versions saved, preventing the version history from becoming excessively large and impacting performance.

Setting the version history limit is crucial for optimizing storage usage and ensuring efficient document management. It allows you to strike a balance between preserving document history and maintaining a manageable versioning system.

Before we proceed, please ensure that you have the following prerequisites in place:

Uninstall the Legacy SharePoint PowerShell Module

1. Open a PowerShell command prompt with administrative privileges. To do this, right-click on the Start button and select “Windows PowerShell (Admin)” or “Windows PowerShell” if you’re using an older version of Windows.

2. To uninstall the module, use the following command

Uninstall-Module -Name Microsoft.SharePoint.PowerShell
Uninstall-Module -Name Microsoft.OnlineSharePoint.PowerShell

3. Confirm the uninstallation by typing “Y” or “A” when prompted.

4. That’s it! The Legacy SharePoint PowerShell module should now be uninstalled from your system.

Installing the PowerShell 7.0 MSI package

PowerShell 7

To install PowerShell on Windows, use the following links to download the install package from GitHub.

Install Latest SharePoint Online PNP PowerShell

Install the New PnP PowerShell Module by running the command in PowerShell 7.0

Install-Module PnP.PowerShell 

Once you have the prerequisites ready, follow the steps below.

PowerShell Script to Set Version History Limit for SharePoint Site

To set the version history limit for an entire SharePoint site, you can utilize PowerShell scripting. Follow the steps below to create the script:

Step 1: Launch PowerShell

Open PowerShell with administrative privileges to ensure you have the necessary permissions to execute the script.

Step 2: Install SharePoint Online Management Shell

Use the following command on PowerShell 7 to install SharePoint Online Management Shell.

# Install SharePoint Online Management Shell module if not already installed
Install-Module -Name Microsoft.Online.SharePoint.PowerShell -Force

Step 3: Set Version History Limit

Next, create a PowerShell script file with the following content:

# Import SharePoint Online module
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking

# Connect to SharePoint Online
Connect-SPOService -Url "https://your-domain-admin.sharepoint.com"

# Get all site collections in the SharePoint Online tenant
$siteCollections = Get-SPOSite

# Iterate through each site collection and set the version history limit
foreach ($site in $siteCollections) {
    Write-Host "Setting version history limit for site:" $site.Url

    # Get the root web of the site collection
    $rootWeb = Get-SPOWeb -Identity $site.Url

    # Set the version history limit for all lists and libraries in the root web
    $rootWeb.Lists | ForEach-Object {
        $_.EnableVersioning = $true
        $_.MajorVersionLimit = 100  # Replace 100 with your desired version limit
        $_.Update()
    }
    $rootWeb.Dispose()
}

Make sure to replace “https://your-domain-admin.sharepoint.com” with the URL of your SharePoint admin site. Also, adjust the version limit (currently set to 10) to your desired value.

Step 4: Execute the Script

Save the script file with a “.ps1” extension (e.g., SetVersionHistoryLimit.ps1) and execute it in PowerShell by running the following command:

.\SetVersionHistoryLimit.ps1

The script will iterate through all site collections in your SharePoint Online tenant, set the version history limit to the specified value (10 in the example script), and apply it to all lists and libraries within each site.

Conclusion

Managing the version history limit in SharePoint is crucial for effective document management and storage optimization. By setting the version history limit using the provided PowerShell script, you can efficiently control the number of versions retained across your entire SharePoint site. This ensures a streamlined versioning system while preserving the necessary document revisions. Take advantage of PowerShell’s automation capabilities to simplify the process and maintain an organized SharePoint environment.

I hope this article was helpful!  You can find more here: SharePoint Articles.


Discover more from Patrick Domingues

Subscribe to get the latest posts sent to your email.

author avatar
Patrick Domingues

11 Comments

  1. Two issues with the script:
    1) Microsoft.Online.SharePoint.PowerShell does not work in Powershell 7
    2) Get-SPOWeb does not exist

    1. Hello Naif, sorry to hear that you are having difficulties.
      1. Open powershell7 as admin and run step 2 again.
      2. Get-SPOWeb it just references back to your SharePoint site. Make sure you have the correct URL.

      1. Still no luck:-

        PS C:\Scripts\O365\SharePoint> Install-Module -Name Microsoft.Online.SharePoint.PowerShell -Force
        PS C:\Scripts\O365\SharePoint> Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
        PS C:\Scripts\O365\SharePoint> Get-SPOWeb
        Get-SPOWeb: The term ‘Get-SPOWeb’ is not recognized as a name of a cmdlet, function, script file, or executable program.
        Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
        PS C:\Scripts\O365\SharePoint>
        PS C:\Scripts\O365\SharePoint> $PSVersionTable

        Name Value
        —- —–
        PSVersion 7.3.6
        PSEdition Core
        GitCommitId 7.3.6
        OS Microsoft Windows 10.0.19045
        Platform Win32NT
        PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
        PSRemotingProtocolVersion 2.3
        SerializationVersion 1.1.0.1
        WSManStackVersion 3.0

  2. Getting the following error when trying to sign in: Connect-SPOService: No valid OAuth 2.0 authentication session exists

    1. Hello Alex,

      Are you using PowerShell v7? Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking , will prompt you for your ms365 admin account credentials, it may prompt you to enter MFA. If you are having issues with MFA you may want to exclude MFA. I suggest creating a new admin account with admin access to SharePoint only and testing the admin login with the new account. If you are using conditional access policies, you may need to exclude the new account for any policies you may have enforcing mfa.

  3. Hi Patrick

    I would like to confirm if this script can change the limit to keep a maximum of 10 file versions, because if I try to change the library to 10 it reports that the smallest number I can leave configured is 100. Could you confirm this?

    I tried to approve it with the script, but I’m having problems, if you could help me I’d appreciate it. I installed PowerShell v7 (x64) – Version 7.4.0, then created a global admin account without MFA.

    When I run the command
    Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking

    It works perfectly, now when I run the command Connect-SPOService -Url “https://your-domain-admin.sharepoint.com”, (of course I change it to my sharepoint domain), it asks for the credentials, I enter the global admin account without MFA and displays the error

    Connect-SPOService: The remote server returned an error: (400) Bad Request.

    Could you tell me why this error is occurring?

  4. The term ‘Get-SPOWeb’ is not recognized as a name of a cmdlet, function, script file, or executable program.
    Check the spelling of the name, or if a path was included, verify that the path is correct and try again

    You can help-me please

  5. The property ‘EnableVersioning’ cannot be found on this object. Verify that the property exists and can be set.
    InvalidOperation:
    Line |
    10 | $_.MajorVersionLimit = 100 # Replace 100 with your desired v …
    | ~~~~~~~~~~~~~~~~~~~~~~~~~~
    | The property ‘MajorVersionLimit’ cannot be found on this object. Verify that the property exists and can be set.
    InvalidOperation:

  6. Hi, I’m going to try to use this script to set the versioning in my Enviroment.

    I read in Microsoft Documentation the minimum number for versioning is 100, Also I do it one by one with a couple of site and work. now I try to set all my sites with this script.

Leave a Comment

Stay Informed

Receive instant notifications when new content is released.