SharePoint Tutorials

How To Run SharePoint File Version Cleanup For Multiple Sites Using PowerShell And CSV

Learn how to efficiently manage file versions in SharePoint document libraries across multiple sites using a PowerShell script that reads site URLs and library names from a CSV file.


Introduction

SharePoint

Managing file versions in SharePoint document libraries can be a daunting task, especially when dealing with multiple sites. Automating this process not only saves time but also ensures consistency across different SharePoint sites. In this article, we’ll explore a PowerShell script that simplifies this task by reading site URLs and library names from a CSV file and performing cleanup operations on each.

Before we proceed, please ensure that you have the following prerequisites in place:

Uninstall the Legacy SharePoint PowerShell Module

1. Open a PowerShell command prompt with administrative privileges. To do this, right-click on the Start button and select “Windows PowerShell (Admin)” or “Windows PowerShell” if you’re using an older version of Windows.

2. To uninstall the module, use the following command

Uninstall-Module -Name Microsoft.SharePoint.PowerShell
Uninstall-Module -Name Microsoft.OnlineSharePoint.PowerShell

3. Confirm the uninstallation by typing “Y” or “A” when prompted.

4. That’s it! The Legacy SharePoint PowerShell module should now be uninstalled from your system.

Installing the PowerShell 7.0 MSI package

PowerShell 7

To install PowerShell on Windows, use the following links to download the install package from GitHub.

Install Latest SharePoint Online PNP PowerShell

Install the New PnP PowerShell Module by running the command in PowerShell 7.0

Install-Module PnP.PowerShell

Once you have the prerequisites ready, follow the steps below.

Step 1: Register Azure AD Application and Grant Access to the Tenant

1. If you havent done so already, register an Azure AD Application in the Azure portal.

Register-PnPManagementShellAccess

2. Grant appropriate permissions to the registered application to access SharePoint Online.

Step 2: Preparing the CSV File

Before running the script, prepare a CSV file with two columns: SiteURL and LibraryName. Each row in this file should correspond to a SharePoint site and its document library. Here’s an example format:

How to run SharePoint File Version Cleanup with PowerShell and CSV

Step 3: SharePoint PowerShell Script for File Cleanup

The script connects to each site listed in the CSV file and performs version cleanup in the specified document libraries. It includes a function Cleanup-Versions that processes each folder within the library recursively, deleting all file versions.

# Import the CSV file containing SiteURLs and LibraryNames
$Sites = Import-Csv "path_to_your_csv_file.csv"

# Parameters
$VersionsToKeep = 1  # Set the number of versions to keep. Change this number as needed.

# Function to clear all file versions except the latest specified number, folder by folder within the library.
Function Cleanup-Versions([Microsoft.SharePoint.Client.Folder]$Folder)
{
    Write-host "Processing Folder:" $Folder.ServerRelativeUrl -f Yellow
    # Get the Site Relative URL of the folder
    $FolderSiteRelativeURL = $Folder.ServerRelativeURL.Replace($Web.ServerRelativeURL, [string]::Empty)

    # Get all files from the folder
    $Files = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType File

    # Iterate through each file
    ForEach ($File in $Files)
    {
        # Get file versions
        $Versions = Get-PnPProperty -ClientObject $File -Property Versions
        Write-host -f White "`tScanning File:" $File.Name

        If ($Versions.Count -gt $VersionsToKeep) # Check if there are more versions than we want to keep
        {
            # Delete versions except the specified number of latest versions
            $Versions | Sort-Object -Property Created -Descending | Select-Object -Skip $VersionsToKeep | ForEach-Object {
                $_.DeleteObject()
            }
            Invoke-PnPQuery
            Write-Host -f Green "`t`tVersions of the File Kept: $VersionsToKeep - Deleted older versions of:" $File.Name
        }
    }

    # Get sub-folders from the folder - Exclude "Forms" and hidden folders
    $SubFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType Folder | Where {($_.Name -ne "Forms") -and (-Not($_.Name.StartsWith("_")))}
    Foreach ($SubFolder in $SubFolders)
    {
        # Call the function recursively
        Cleanup-Versions -Folder $SubFolder
    }
}

# Loop through each site from the CSV file
Foreach ($Site in $Sites) {
    # Set the current site URL and library name
    $SiteURL = $Site.SiteURL
    $LibraryName = $Site.LibraryName

    # Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive
    $Web = Get-PnPWeb

    # Get the root folder of the library
    $RootFolder = Get-PnPList -Identity $LibraryName -Includes RootFolder | Select -ExpandProperty RootFolder

    # Call the function with the root folder of the library
    Cleanup-Versions -Folder $RootFolder
}

Functionality of the Script

  1. CSV File Import: The script starts by importing a CSV file that contains the SiteURL and LibraryName for each SharePoint site you want to process.
  2. Cleanup Function: The Cleanup-Versions function is the core of the script. It:
    • Processes each folder in the specified document library.
    • Retrieves all files and their versions.
    • Keeps only the latest version of each file, deleting all older versions.
  3. Recursive Folder Processing: The function is designed to work recursively, meaning it will process not only the files in the root of the document library but also those in all sub-folders, excluding “Forms” and hidden folders.
  4. Execution Loop: After defining the function, the script loops through each row in the CSV file, connects to the corresponding SharePoint site, retrieves the root folder of the specified library, and calls the Cleanup-Versions function for it.

Customization Points

  1. CSV File Path: You need to replace "path_to_your_csv_file.csv" with the actual file path of your CSV. Make sure your CSV file is formatted with two columns, SiteURL and LibraryName, each containing the URL of a SharePoint site and the name of a document library within that site, respectively.
  2. Connection Method: The script uses Connect-PnPOnline -Url $SiteURL -Interactive for connecting to SharePoint. Depending on your authentication method, you might need to modify this part.
  3. Script Execution: Before running the script, ensure that you have the required permissions to access and modify the document libraries in the specified SharePoint sites.

Parameter for Number of Versions to Keep

  1. Modify the Cleanup Function: Update the Cleanup-Versions function to use the $VersionsToKeep parameter when deciding how many versions to delete.
  2. Number of Versions to Keep: Change the value of $VersionsToKeep according to how many recent versions of each file you wish to retain. For example, if you set $VersionsToKeep = 3, the script will keep the three most recent versions of each file and delete the rest.

Conclusion

This script provides a streamlined approach to handling file version cleanup across multiple SharePoint sites. By leveraging a CSV file, it allows for easy scalability and adaptability to various SharePoint environments, making it a valuable tool for SharePoint administrators and IT professionals.

I hope this article was helpful! You can find more here: SharePoint Articles.

author avatar
Patrick Domingues

Leave a Comment

Stay Informed

Receive instant notifications when new content is released.