Skip to content

Instantly share code, notes, and snippets.

@junecastillote
Created September 6, 2023 08:33
Show Gist options
  • Save junecastillote/c4026257bfcae201284dde4292f4cb8e to your computer and use it in GitHub Desktop.
Save junecastillote/c4026257bfcae201284dde4292f4cb8e to your computer and use it in GitHub Desktop.
Set SPO site versioning limit using PnP PowerShell
[CmdletBinding()]
param (
[Parameter(Mandatory)] [string[]] $SiteURL,
[parameter(Mandatory)] [int]$VersioningLimit,
[parameter(Mandatory)] [string]$ApplicationId,
[parameter(Mandatory)] [string]$Thumbprint,
[parameter(Mandatory)] [string]$Tenant,
[parameter()] [bool] $TestMode = $true
)
Function Say {
param(
[Parameter(Mandatory)]
$Text,
[Parameter()]
$Color = 'Cyan'
)
# $originalForegroundColor = $Host.UI.RawUI.ForegroundColor
if ($Color) {
$Host.UI.RawUI.ForegroundColor = $Color
}
$Text | Out-Default
[Console]::ResetColor()
}
# Filter out the portal, admin portal, and search site URLs.
$urlPatternToExclude = ".*-my\.sharepoint\.com/$|.*\.sharepoint\.com/$|.*\.sharepoint\.com/search$"
$SiteURL = $SiteURL | Where-Object { $_ -notmatch $urlPatternToExclude }
# System libraries to ignore
$SystemLibraries = @("Form Templates", "Pages", "Preservation Hold Library", "Site Assets", "Site Pages", "Images",
"Site Collection Documents", "Site Collection Images", "Style Library")
# Process each site
for ($urlIndex = 0 ; $urlIndex -lt $SiteURL.Count ; $urlIndex++) {
$url = $SiteURL[$urlIndex]
try {
Say "Site $($urlIndex+1) of $($SiteURL.Count): [$($url)]" Cyan
Connect-PnPOnline -Url $url -ClientId $ApplicationId -Thumbprint $Thumbprint -Tenant $Tenant -ErrorAction Stop
$site = Get-PnPTenantSite -Identity $url
if ($url -like "*-my.sharepoint.com*") {
Say " -> OneDrive Name: $($site.Title)" Yellow
}
else {
Say " -> Site Name: $($site.Title)" Yellow
}
# Get all document libraries that are:
# * BaseType is DocumentLibrary
# * Not hidden
# * Title is not in the $SystemLibraries
# * Versioning is enabled
# * Current versioning limit is different from $VersioningLimit
$DocumentLibraries = @(
Get-PnPList |
Where-Object {
$_.BaseType -eq "DocumentLibrary" -and
$_.Hidden -eq $False -and
$_.Title -notin $SystemLibraries -and
$_.EnableVersioning -and
$_.MajorVersionLimit -ne $VersioningLimit
}
)
if ($DocumentLibraries.Count -gt 0) {
# Process each document library
for ($libraryIndex = 0; $libraryIndex -lt $DocumentLibraries.Count; $libraryIndex++) {
$library = $DocumentLibraries[$libraryIndex]
Say " -> Library name: [$($library.Title)]" Magenta
if (!$TestMode) {
# If $TestMode = False, change the versioning limit.
$null = Set-PnPList -Identity $library.Id -MajorVersions $VersioningLimit -ErrorAction Stop
Say " -> Version limit settings updated to $VersioningLimit" Green
}
else {
# If $TestMode = True (default), simulate changing the versioning limit.
Say " -> [TEST-MODE] Version limit settings updated to $VersioningLimit" Green
}
}
}
else {
Say " -> No document libraries needing update." Magenta
}
}
catch {
Say "[ERROR] - [$($site.Title)]: $($_.Exception.Message)" Red
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment