Powershell to enable Versioning for every list and library in a given sharepoint site

Update – A newer version of this script exists here- I’ve left this one here for search results, and because it’s scoped at the web level, whereas the new one is scoped at the IIS level.

This script will enable Versioning for every list and document library in a given sharepoint site.

I’d originally written it to just do document libraries, but changed my mind – things like announcements and calendars are lists, and they are equally important to protect. Same with some of the default libraries like “Site Assets”

Note that if the script sees that versioning is already enabled, it tells you – so no harm running the script more than once.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue
$web = get-spweb "http://www.yoursharepointurl.com/sites/yoursite/"
$lists = $web.lists
foreach ($list in $lists)
{
    if($list.EnableVersioning -eq $false)
    {
         write-host $list.title "is a not using versions"
         $list.Enableversioning = $true
         $List.MajorVersionLimit = 5
         $list.update()
    }
    else
    {
         Write-host $list.title " is set for up to " $list.MajorVersionLimit "previous versions"
    }
}