Last active
June 12, 2024 02:28
-
-
Save Offbeatmammal/d52b7ab1d6308cfdfb9494be52695fd4 to your computer and use it in GitHub Desktop.
We had an issue where the default number of versions in SharePoint was consuming a lot of space. We were able to reset the default, but there isn't a quick way to prune the old versions across and entire site (have to do it file by file) so built a quick and dirty PowerShell script we could use to handle one or more sites, limited to only PowerP…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Ensure you have installed PnP Powershell | |
# https://pnp.github.io/powershell/articles/installation.html | |
# Set Variables | |
$SiteURL = "https://{URL}.sharepoint.com/sites/" | |
# ragged array of two or three parameters: | |
# - the name of the site (to be concatenated with the $SiteURL), | |
# - the root folder of the site to prune | |
# - optionally any message to display to the user while the script runs for that folder | |
$Sites = ("{site}","{root folder}"),("{site}","{root folder}","{comment to display}"),("{site}","root folder}") | |
$VersionsToKeep = 10 | |
foreach($site in $sites) | |
{ | |
$url = $SiteURL+$site[0]+"/" | |
#Connect to PnP Online | |
Connect-PnPOnline -Url $url -Interactive | |
Write-Host -f Green "Please wait, getting folder/file list for" $site[0] | |
if ($site.Count -eq 3) | |
{ | |
Write-Host -f Cyan $site[2] | |
} | |
$ListName = "/"+$site[1] | |
$ListItems = Get-PnPFolderItem -FolderSiteRelativeUrl $ListName -ItemType File -Recursive | |
Write-Host -f Green "Processing list for any documents with more than" $VersionsToKeep "versions" | |
#Loop through each Item | |
$cnt = 0 | |
foreach($File in $ListItems) | |
{ | |
$cnt += 1 | |
$i = [math]::ceiling($cnt * 100 / $ListItems.Count) | |
Write-Progress -Activity "Processing Files" -Status "$i% Complete:" -PercentComplete $i | |
if (($File.Name.Length -gt 6) -and | |
(($File.Name.Substring($File.Name.Length - 5) -eq ".pptx") -or ($File.Name.Substring($File.Name.Length - 5) -eq ".docx"))) | |
{ | |
$Versions = Get-PnPProperty -ClientObject $File -Property versions | |
$VersionsCount = $Versions.Count | |
$VersionsToDelete = $VersionsCount - $VersionsToKeep + 1 | |
If($VersionsToDelete -gt 0) | |
{ | |
Write-Host "File:" $File.ServerRelativeUrl "[" $VersionsCount "]" | |
Write-Host -f Cyan "`t Total Number of Versions to be deleted:" $VersionsToDelete | |
#Delete versions | |
For($i=0; $i -lt $VersionsToDelete; $i++) | |
{ | |
If ($Versions[$i].IsCurrentVersion) | |
{ | |
$VersionsToDelete++ | |
Continue | |
} | |
Write-Host -f Cyan "`t Deleting Version:" $Versions[$i].VersionLabel | |
Remove-PnPFileVersion -URL $File.ServerRelativeUrl -Identity $Versions[$i].ID -Force | |
} | |
Write-Host -f Green "`t Version History is cleaned for the File:" $File.Name | |
} | |
} | |
} | |
Write-Progress -Activity "Processing Files" -Completed | |
} | |
Disconnect-PnPOnline |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment