Managing OneDrive Version History using PnP PowerShell

Loading

Managing OneDrive for Business Version History is crucial for storage optimization, compliance, and data recovery. Using PnP PowerShell, you can:
✔ Enable/disable versioning for OneDrive files
✔ Set version limits for efficient storage use
✔ Delete old versions to free up space
✔ Generate reports on file versions

This guide provides step-by-step instructions for managing OneDrive version history using PnP PowerShell.


Step 1: Install and Update PnP PowerShell

Ensure you have PnP PowerShell installed. Open PowerShell as Administrator and run:

Install-Module -Name PnP.PowerShell -Force -AllowClobber

To update the module:

Update-Module -Name PnP.PowerShell

Verify the installation:

Get-Module -Name PnP.PowerShell -ListAvailable

Step 2: Connect to OneDrive Using PnP PowerShell

Since OneDrive for Business is managed via SharePoint Online, connect to the SharePoint Admin Center:

$adminUrl = "https://yourtenant-admin.sharepoint.com"
Connect-PnPOnline -Url $adminUrl -Scopes "Sites.FullControl.All" -Interactive

For App-based authentication, use:

$clientId = "your-client-id"
$tenantId = "your-tenant-id"
$clientSecret = "your-client-secret"

Connect-PnPOnline -Url $adminUrl -ClientId $clientId -ClientSecret $clientSecret -Tenant $tenantId

Step 3: Get OneDrive Sites

To list all OneDrive sites in your organization:

$oneDriveSites = Get-PnPTenantSite -IncludeOneDriveSites | Where-Object { $_.Url -like "*-my.sharepoint.com/personal/*" }

$oneDriveSites | Select-Object Url, Owner | Format-Table -AutoSize

OneDrive Site URL
Site Owner


Step 4: Check Version History Settings

To retrieve the current versioning settings for a user’s OneDrive document library:

$oneDriveUrl = "https://yourtenant-my.sharepoint.com/personal/user_domain_com"
Connect-PnPOnline -Url $oneDriveUrl -Interactive

$library = Get-PnPList -Identity "Documents"

Write-Host "Major Versions: $($library.MajorVersionLimit)"
Write-Host "Minor Versions: $($library.MajorWithMinorVersionsLimit)"
Write-Host "Require Checkout: $($library.ForceCheckout)"

Major Version Limit
Minor Version Limit
Require Check-out Status


Step 5: Enable or Modify Versioning Settings

To enable versioning and set limits:

Set-PnPList -Identity "Documents" -EnableVersioning $true -MajorVersions 500 -MinorVersions 50 -ForceCheckout $false

Write-Host "Versioning enabled with 500 major versions and 50 minor versions."

Step 6: Delete Old Versions

To delete all previous versions from a OneDrive library:

$files = Get-PnPListItem -List "Documents"

foreach ($file in $files) {
Remove-PnPFileVersion -List "Documents" -Identity $file.FieldValues.FileRef -Force
}

Write-Host "All previous file versions deleted."

Delete Versions Older Than 6 Months

$cutoffDate = (Get-Date).AddMonths(-6)

foreach ($file in $files) {
$versions = Get-PnPFileVersion -Url $file.FieldValues.FileRef
foreach ($version in $versions) {
if ($version.Created -lt $cutoffDate) {
Remove-PnPFileVersion -List "Documents" -Identity $file.FieldValues.FileRef -Force
}
}
}

Write-Host "Versions older than 6 months deleted."

Step 7: Export Version History Report

To generate a report of all files and their version history:

$reportPath = "C:\Reports\OneDrive_VersionHistory_Report.csv"
$versionData = @()

foreach ($file in $files) {
$versions = Get-PnPFileVersion -Url $file.FieldValues.FileRef
foreach ($version in $versions) {
$versionData += [PSCustomObject]@{
FileName = $file.FieldValues.FileLeafRef
VersionLabel = $version.VersionLabel
Created = $version.Created
CreatedBy = $version.CreatedBy
}
}
}

$versionData | Export-Csv -Path $reportPath -NoTypeInformation

Write-Host "OneDrive Version History Report saved to $reportPath"

Step 8: Automate Version History Management

1. Open Task Scheduler

  • Click Start, search for Task Scheduler, and open it.
  • Click Create Basic Task.
  • Name it “OneDrive Version Cleanup”.

2. Set Trigger

  • Choose Weekly or another frequency.
  • Set the execution time.

3. Set Action

  • Select Start a Program.
  • In Program/Script, enter: powershell.exe
  • In Arguments, enter: -File "C:\Scripts\OneDriveVersionCleanup.ps1"
  • Click Finish.

This automates OneDrive version cleanup and reporting.

Leave a Reply

Your email address will not be published. Required fields are marked *