1

I am wondering if you can possibly help me with an issue i am having. I have managed to get a powershell script for enabling versioning control on a site in Sharepoint and it seems to work fine for the most part.

However what i ideally want the script to do is enable versioning for that site and any subsequent subsites that then site under it. I cannot however get it working, have tried a number of things but to no avail.

I am no expert and hoping one of you guys can help out!

Appreciate any and all help.

Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue

$webURL= "URL HERE"
#Get the Web
$web = Get-SPWeb $webURL

#Get all lists - Exclude System lists
$ListColl = $web.lists | Where-Object  { ($_.hidden -eq $false) -and 
($_.IsSiteAssetsLibrary -eq $false) 
}

foreach($list in $ListColl)
 {
if ($list.BaseType -eq "DocumentLibrary")
{
  Write-Host "Versioning enabled: " $list.EnableVersioning
  $host.UI.WriteLine()
  #Write-Host "MinorVersioning Enabled: " $list.EnableMinorVersions
  #$host.UI.WriteLine()
  #Write-Host "EnableModeration: " $list.EnableModeration
  #$host.UI.WriteLine()
  Write-Host "Major Versions: " $list.MajorVersionLimit
  $host.UI.WriteLine()
  #Write-Host "Minor Versions: " $list.MajorWithMinorVersionsLimit
  #$host.UI.WriteLine()
  $list.EnableVersioning = $true
  #Disable creation of minor (draft) versions (document libraries only)
  $list.EnableMinorVersions = $false
  $list.MajorVersionLimit = 5
  #$list.MajorWithMinorVersionsLimit = 5
  $list.Update()
  Write-Host $list.Title "is updated with MajorVersionLimit 5 "
 }
}
0

This could be solved with recursive function that would go through all sites and their subsites (and their subsites...) and execute your code for enabling the versioning. This should work fine:

Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue

$webURL= "URL HERE"
$web = Get-SPWeb $webURL

function PSEnableVersioning {
  Param($Web)
    $ListColl = $web.lists | Where-Object  { ($_.hidden -eq $false) -and ($_.IsSiteAssetsLibrary -eq $false) }
    foreach($list in $ListColl)
    {
        if ($list.BaseType -eq "DocumentLibrary")
        {
            Write-Host "Versioning enabled: " $list.EnableVersioning
            $host.UI.WriteLine()
            #Write-Host "MinorVersioning Enabled: " $list.EnableMinorVersions
            #$host.UI.WriteLine()
            #Write-Host "EnableModeration: " $list.EnableModeration
            #$host.UI.WriteLine()
            Write-Host "Major Versions: " $list.MajorVersionLimit
            $host.UI.WriteLine()
            #Write-Host "Minor Versions: " $list.MajorWithMinorVersionsLimit
            #$host.UI.WriteLine()
            $list.EnableVersioning = $true
            #Disable creation of minor (draft) versions (document libraries only)
            $list.EnableMinorVersions = $false
            $list.MajorVersionLimit = 5
            #$list.MajorWithMinorVersionsLimit = 5
            $list.Update()
            Write-Host $list.Title "is updated with MajorVersionLimit 5 "
        }
    }

    $SubWebs = $Web.Webs
    foreach($SubWeb in $SubWebs)
    {
        PSEnableVersioning $SubWeb
    }
}

PSEnableVersioning $web
  • Thank you Damjan, that worked perfectly! Does exactly what is needed. – Omar Feb 21 '18 at 8:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.