SharePoint Versioning: the Powershell perspective (part 3)

Tweaks and variations

See also

The previous parts presented a detailed description of how to enable minor and major versions as well as major versioning for lists and libraries. They described the difference for this scripts that lists and libraries play and how to serialize the the cmdlet in order to target all lists in libraries in one site.

Obviously, all scripts can be tweaked more and less and you are very welcome to add your ideas and versions here in this article.

Below you can find several ideas.

Loop through all sites in a site collection

As a basis for this script, one of the scripts from part 2 is used. You can choose a script for minor versions or only major versions depending on the purpose of the script. It needs a little addition in order to loop through all the sites in a site collection.

First load the subsites.

$ctx.Load($ctx.Web.Webs)
  $ctx.ExecuteQuery()

Once we have the sites we could copy-paste the code to a foreach loop and loop through them in order to change the settings for the lists of those sites, but that wouldn't be very efficient. First of all, copy-paste is not a very good practice for code writing and should be avoided. Secondly, what about subsites within subsites? Or sub-sub-subsites? You can have up to 2000 subsites within one site collection at different levels.

That's why the script uses recursion in order to loop through the sites in a site collection and if they exist (Count -gt 0) change the versioning setting for their lists and check again if they in turn have any subsites of their own. 

if($ctx.Web.Webs.Count -gt 0)
  {
    for($i=0; $i -lt   $ctx.Web.Webs.Count ; $i++) 
    {
        getall($ctx.Web.Webs[$i].Url) 
    }

 

The full script can be downloaded here or for minor versions here.

Loop through all sites in all site collection in the tenant

Take the script for above and ask it to loop through all site collection as well. Once it goes into a site collection, it will loop through its subsites. When all subsites have been processed, the script will enter another site collection:

$username = Read-Host -Prompt "Enter admin's login, e.g. admin@domain.onmicrosoft.com"
 
$password = Read-Host -Prompt "Enter password" -AsSecureString
$credy= New-Object System.Management.Automation.PSCredential($username,$password)  
Connect-SPOService -Credential $credy -Url  $siteUrl
   
$sitecollections=get-SPOSite
   
foreach($sitecoll in $sitecollections)
{ 
    getall($sitecoll.Url)
}

The full script can be downloaded here or for minor versions here.

Enable minor versions where possible and major versions in other lists 

In that situation, you can run first the script to enable major versions in all lists and then the script to enable minor and major versions. For those lists that support it, minor versions will be enabled. For those that not - the setting will stay on major versions.