SharePoint – Enabling Versioning and Version History in all Document Libraries using PowerShell

The script below can be used in order to enable versioning in all SharePoint document libraries and also to set how many major versions to retain and how many minor versions to maintain.

it’s important to keep in mind when choosing how many minor versions to retain that the setting actually specifies how many ‘Major Versions’ drafts’ should be retained and not a hard limit on how many ‘draft versions’ will be kept.



This has to be run on the SharePoint server by a user with at least contribute rights on all affected libraries.

$ErrorActionPreference = 'silentlycontinue'

Add-PSSnapin Microsoft.SharePoint.PowerShell

# Variables
$siteCollection = "https://*SharePointUrl"
$majorVersionCount = 5 # Sets the limit on how many major version to retain
$majorVersionsDraftsCount = 1 # Sets the limit on how many major versions we should keep all drafts of
$exclusions = @("Customized Reports", "Form Templates", "Translation Packages", "Converted Forms", "Master Page Gallery", "Reusable Content", "Site Assets", "Site Collection Documents", "Site Collection Images", "Style Library", "Suggested Content Browser Locations", "User Information List", "wfpub", "Pages", "Site Pages")

$foundLibraries = @()
$countOfFoundLibraries = 0

# Get the Site Collection
$Site = Get-SPSite $siteCollection
$spWebApp = $Site.WebApplication

write-host "Locating all Libraries that will be affected by this change.." -foregroundcolor "yellow"

# Locate all lists in all sites, filtering as required
foreach ($allSites in $spWebApp.Sites) {
    foreach ($Web in $allSites.AllWebs) { # loop through all sites
        foreach ($list in $Web.Lists) { # loop through every list in sites
            if ((-not ($exclusions -contains $list.Title)) -and ($list.BaseTemplate -eq "DocumentLibrary")) { 
            # If the found list's name isn't in the exclusions list and it's a document library
                $foundLibraries += $list # Add it to the foundLibraries array

# Show all found lists
write-host "Found the following libraries -" -foregroundcolor "yellow"
$foundLibraries | format-Table -Property Title, @{Label="URL"; Expression={$_.ParentWeb.Url + "/" + $_.RootFolder.Url}} -AutoSize

# Confirm that lists will be modified
write-host "This will enable version history on $countOfFoundLibraries libraries, confirm? (Y/N)`n" -foregroundcolor "yellow"
$confirmation = Read-Host

# If user confirmed
if ($confirmation -eq 'y' -or 'Y') {
    # Loop through all lists and update title
    foreach ($item in $foundLibraries) {
        try {
            $url = $item.ParentWeb.Url + "/" + $item.RootFolder.Url
            $list.EnableVersioning = $true  # Enabled document versioning
            $item.EnableMinorVersions = $true # Enables minor (draft) versioning of documents

            $item.MajorVersionLimit = $majorVersionsCount # Sets the limit on how many major version to retain
            $item.MajorWithMinorVersionsLimit = $majorVersionsDraftsCount # Sets the limit on how many major versions we should keep all drafts of

            $item.Update() # Updates the list
            write-host "Updated $url library sucessfully" -foregroundcolor "Green"
        } catch {
            write-host "Failed to update $url library" -foregroundcolor "Red"
} else {
    write-host "Libraries will not be updated" -foregroundcolor "yellow"


Any issues don’t hesitate to comment below or contact me directly.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.