Enabling Document Library Versioning via PowerShell

I got a request the other day for a way to auto-enable versioning for a lot of SharePoint Document Libraries. Since document versioning is on a per library basis enabling is typically done after the fact by the creator through the user interface.

Here is a simple Windows PowerShell script that will enable versioning on all lists that are BaseType = DocumentLibrary. Enabling versioning on every single Document Library is not the wisest thing to do for a Farm but you can take this script as a basis and then customize to enable based on a Name, particular Site(s) or whatever other criteria you would like.

function Set-DocLibVersioning() {
    $farm = [Microsoft.SharePoint.Administration.SPFarm]::Local
    foreach ($spService in $farm.Services) {
        if (!($spService -is [Microsoft.SharePoint.Administration.SPWebService])) {

        foreach ($webApp in $spService.WebApplications) {
            if ($webApp -is [Microsoft.SharePoint.Administration.SPAdministrationWebApplication]) { continue }

            foreach ($site in $webApp.Sites) {
                foreach ($web in $site.AllWebs) {
                Write-Host "Site = " $site
                    foreach ($list in $web.Lists) {
                        if ($list.BaseType -eq "DocumentLibrary") {
                                Write-Host "List = " $list
                                # Enable Versioning
                                $list.EnableVersioning = $true