2.3 C
New York
Sunday, January 14, 2024

How To Run SharePoint File Model Cleanup For A number of Websites Utilizing PowerShell And CSV

Discover ways to effectively handle file variations in SharePoint doc libraries throughout a number of websites utilizing a PowerShell script that reads web site URLs and library names from a CSV file.



Managing file variations in SharePoint doc libraries is usually a daunting process, particularly when coping with a number of websites. Automating this course of not solely saves time but in addition ensures consistency throughout completely different SharePoint websites. On this article, we’ll discover a PowerShell script that simplifies this process by studying web site URLs and library names from a CSV file and performing cleanup operations on every.

Earlier than we proceed, please guarantee that you’ve the next stipulations in place:

Uninstall the Legacy SharePoint PowerShell Module

1. Open a PowerShell command immediate with administrative privileges. To do that, right-click on the Begin button and choose “Home windows PowerShell (Admin)” or “Home windows PowerShell” if you happen to’re utilizing an older model of Home windows.

2. To uninstall the module, use the next command

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

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

4. That’s it! The Legacy SharePoint PowerShell module ought to now be uninstalled out of your system.

Putting in the PowerShell 7.0 MSI bundle

PowerShell 7

To put in PowerShell on Home windows, use the next hyperlinks to obtain the set up bundle from GitHub.

Set up Newest SharePoint On-line PNP PowerShell

Set up the New PnP PowerShell Module by working the command in PowerShell 7.0

Set up-Module PnP.PowerShell

Upon getting the stipulations prepared, comply with the steps under.

Step 1: Register Azure AD Utility and Grant Entry to the Tenant

1. Should you havent executed so already, register an Azure AD Utility within the Azure portal.


2. Grant acceptable permissions to the registered software to entry SharePoint On-line.

Step 2: Getting ready the CSV File

Earlier than working the script, put together a CSV file with two columns: SiteURL and LibraryName. Every row on this file ought to correspond to a SharePoint web site and its doc library. Right here’s an instance format:

How to run SharePoint File Version Cleanup with PowerShell and CSV

Step 3: SharePoint PowerShell Script for File Cleanup

The script connects to every web site listed within the CSV file and performs model cleanup within the specified doc libraries. It features a perform Cleanup-Variations that processes every folder throughout the library recursively, deleting all file variations.

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

# Parameters
$VersionsToKeep = 1  # Set the variety of variations to maintain. Change this quantity as wanted.

# Perform to clear all file variations besides the most recent specified quantity, folder by folder throughout the library.
Perform Cleanup-Variations([Microsoft.SharePoint.Client.Folder]$Folder)
    Write-host "Processing Folder:" $Folder.ServerRelativeUrl -f Yellow
    # Get the Web site Relative URL of the folder
    $FolderSiteRelativeURL = $Folder.ServerRelativeURL.Change($Internet.ServerRelativeURL, [string]::Empty)

    # Get all information from the folder
    $Information = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType File

    # Iterate via every file
    ForEach ($File in $Information)
        # Get file variations
        $Variations = Get-PnPProperty -ClientObject $File -Property Variations
        Write-host -f White "`tScanning File:" $File.Identify

        If ($Variations.Rely -gt $VersionsToKeep) # Verify if there are extra variations than we need to hold
            # Delete variations besides the desired variety of newest variations
            $Variations | Kind-Object -Property Created -Descending | Choose-Object -Skip $VersionsToKeep | ForEach-Object {
            Write-Host -f Inexperienced "`t`tVersions of the File Saved: $VersionsToKeep - Deleted older variations of:" $File.Identify

    # Get sub-folders from the folder - Exclude "Types" and hidden folders
    $SubFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType Folder | The place {($_.Identify -ne "Types") -and (-Not($_.Identify.StartsWith("_")))}
    Foreach ($SubFolder in $SubFolders)
        # Name the perform recursively
        Cleanup-Variations -Folder $SubFolder

# Loop via every web site from the CSV file
Foreach ($Web site in $Websites)  Choose -ExpandProperty RootFolder

    # Name the perform with the foundation folder of the library
    Cleanup-Variations -Folder $RootFolder

Performance of the Script

  1. CSV File Import: The script begins by importing a CSV file that incorporates the SiteURL and LibraryName for every SharePoint web site you need to course of.
  2. Cleanup Perform: The Cleanup-Variations perform is the core of the script. It:
    • Processes every folder within the specified doc library.
    • Retrieves all information and their variations.
    • Retains solely the most recent model of every file, deleting all older variations.
  3. Recursive Folder Processing: The perform is designed to work recursively, that means it’ll course of not solely the information within the root of the doc library but in addition these in all sub-folders, excluding “Types” and hidden folders.
  4. Execution Loop: After defining the perform, the script loops via every row within the CSV file, connects to the corresponding SharePoint web site, retrieves the foundation folder of the desired library, and calls the Cleanup-Variations perform for it.

Customization Factors

  1. CSV File Path: It’s worthwhile to substitute "path_to_your_csv_file.csv" with the precise file path of your CSV. Make certain your CSV file is formatted with two columns, SiteURL and LibraryName, every containing the URL of a SharePoint web site and the identify of a doc library inside that web site, respectively.
  2. Connection Methodology: The script makes use of Join-PnPOnline -Url $SiteURL -Interactive for connecting to SharePoint. Relying in your authentication methodology, you may want to change this half.
  3. Script Execution: Earlier than working the script, guarantee that you’ve the required permissions to entry and modify the doc libraries within the specified SharePoint websites.

Parameter for Variety of Variations to Hold

  1. Modify the Cleanup Perform: Replace the Cleanup-Variations perform to make use of the $VersionsToKeep parameter when deciding what number of variations to delete.
  2. Variety of Variations to Hold: Change the worth of $VersionsToKeep in line with what number of current variations of every file you want to retain. For instance, if you happen to set $VersionsToKeep = 3, the script will hold the three most up-to-date variations of every file and delete the remainder.


This script supplies a streamlined strategy to dealing with file model cleanup throughout a number of SharePoint websites. By leveraging a CSV file, it permits for straightforward scalability and adaptableness to numerous SharePoint environments, making it a invaluable instrument for SharePoint directors and IT professionals.

I hope this text was useful! Yow will discover extra right here: SharePoint Articles.

Supply hyperlink

Related Articles


Please enter your comment!
Please enter your name here

Latest Articles