Forum Discussion
"Trim versions" to be aligned with the library version settings
Just wrote a PowerShell script that should get what you want. Note that you should always test any scripts (including this one) in a Test environment first to ensure it works as expected. This script requires special attention to testing since it permanently deletes versions.
It will iterate over all libraries on the site excluding any hidden libraries, and any libraries you add to the exclusion list.
For each library, it will get the current Major and Minor version setting and trim down to that - which mitigates the issue you are currently facing with the overarching SPOSiteFileVersionBatchDeleteJob cmdlet.
This will take in a SiteUrl and Action (either Trim or JobProgress). The SiteUrl will be the site you want to trim libraries on. The Action will either start trimming the libraries or show the progress of the trim job.
Note that you will need to put your current tenant in place of YOURTENANT and your Site Url.
You will need to first switch to the path where the script exists. In my case it is:
cd "C:\Scripts\SPO\Versions"Then run the script using either Trim or JobProgress.
.\Trim-LibraryVersions.ps1 -SiteUrl "https://YOURTENANT.sharepoint.com/sites/YOURSITE" -Action JobProgress
OR
.\Trim-LibraryVersions.ps1 -SiteUrl "https://YOURTENANT.sharepoint.com/sites/YOURSITE" -Action TrimThe script also has a prompt to ensure you entered the correct SiteUrl before proceeding.
<#
SYNOPSIS:
This script trims versions on documents libraries across a site to the current version limit on the library.
PARAMS
- SiteURL - The url of the site you want to trim libraries on
- Action - Trim to trim the versions, or JobProgress to check the progress of the trim job
#>
Param (
[Parameter(Mandatory = $true)]
[string] $SiteUrl = $(Throw "Please specify the Site you wish to trim libraries across."),
[Parameter(Mandatory = $true)]
[ValidateSet("Trim", "JobProgress")]
[string]$Action = $(Throw "Please specify either Trim or JobProgress")
)
function main() {
Connect-SPOService -Url "https://YOURTENANT-admin.sharepoint.com"
$site = Get-SPOSite -Identity $SiteUrl
#Confirm this is the correct site before continuing
Write-Host $site.Title
Write-Host $site.Url
Write-Host ""
Write-Host "Is this site correct?"
Write-Host ""
$result = Read-Host "Press Y to continue, or any other key to exit and start again"
if($result.ToUpper() -ne "Y") { return }
#------------------------------------------------------------------
#Lists you want to exclude
$excludeLists =
"Form Templates",
"Style Library",
"Site Pages"
#Get document libraries that are not hidden and not included in the exlcuded lists
$lists = Get-PnPList | where {$_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $false -and $_.Title -notin $excludeLists}
#Iterate through each list
foreach($list in $lists) {
$listTitle = $list.Title
Write-Host $listTitle -ForegroundColor Yellow
switch ($Action) {
"Trim" {
#Get the Major and Minor Version Limits for the current list
$majorVersionLimit = $list.MajorVersionLimit
$minorVersionLimit = $list.MajorWithMinorVersionsLimit
Write-Host "Major Versions: $($majorVersionLimit)"
Write-Host "Minor Versions: $($minorVersionLimit)"
#Only action lists that have versioning enabled
if($list.EnableVersioning -eq $true) {
New-SPOListFileVersionBatchDeleteJob `
-Site $SiteUrl `
-List $listTitle `
-MajorVersionLimit $majorVersionLimit `
-MajorWithMinorVersionsLimit $minorVersionLimit `
-Confirm:$false
}
}
"JobProgress" {
Get-SPOListFileVersionBatchDeleteJobProgress -Site $SiteUrl -List $listTitle
}
}
""
}
Disconnect-SPOService
}
cls
main
#cd "C:\Scripts\SPO\Versions"
#.\Trim-LibraryVersions.ps1 -SiteUrl "https://YOURTENANT.sharepoint.com/sites/YOURSITE" -Action JobProgress
#.\Trim-LibraryVersions.ps1 -SiteUrl "https://YOURTENANT.sharepoint.com/sites/YOURSITE" -Action Trim