PowerShell script to Major and Minor versions across SharePoint sites and remove out-of-date versions
This script can run in one of two modes
- What If - Non destructive dry run, will report on potential space savings
- Delete Old Versions - Same as above but will actually delete versions in SharePoint
- Edit the CreateSelfSignedCert.ps1 file.
- Replace {certificateName} with something like SharePoint Version Control.
- Navigate to directory in PowerShell and run ./CreateSelfSignedCert.ps1
- Make a note of the random string that gets output (the Thumbprint).
- Sign in to the Azure portal
- Navigate to App Registrations
- New Registration
-
Name your app something like "Sharepoint Version Control"
-
Select the correct account type (normally Single Tenant)
-
Leave Redirect URL empty
-
Click Register
-
Make note of the Application (client) ID
-
Make note of the Directory (tenant) ID
-
Click "View API permissions"
- Click Add a permission
- Click the Sharepoint block
- Click Application permissions
- Check 'Sites.FullControl.All' or 'Sites.Selected' if you only want to look at one SharePoint Site
- Click Add permissions button
- You should see your new permission for SharePoint Listed
- Click 'Grant admin consent'
- upload it
How to setup the config file
- Replace the TenantID Value with the Directory (tenant) ID you noted down in the stage above (line 3).
- Replace the clientId Value with the Application (client) ID you noted down in the stage above (line 6).
- Replace the thumbprint Value with the Thumbprint string you noted down in step 4 of Creating a Certificate (line 11).
- Change majorVersionCount to the number of major version you want to keep, or leave at the default value (line 19)
- Change minorVersionCount to the number of minor version you want to keep, or leave at the default value (line 22)
- Change whatIfMode Value to true
- Change deleteOldMajorVersions Value to false
Steps 4 and 5 are WELL worth double checking, probably best to do a try run first before deleting all your files!
This file holds the SharePoint URLs for the sites you want to process.
- Install PNP module (used so PowerShell can talk to Sharepoint)
Install-Module -Name PnP.PowerShell
- Make sure PowerShell has script execution permissions, follow instructions here.