Friday, May 2, 2025

PowerShell Script to Bulk Update "Next Review Date" for PDFs in SharePoint Online

  In document management scenarios, it's common to have review schedules for files such as policies, SOPs, or manuals. This blog post walks you through a PowerShell script that automates the process of updating the “Next Review Date” field for all PDF documents in a SharePoint Online document library.

We’ll use the PnP PowerShell module to fetch and update items efficiently, while also handling time zone specifics like CST/CDT.

Prerequisites

Before running the script, ensure:

  • You have the PnP PowerShell module installed:
    Install-Module PnP.PowerShell -Scope CurrentUser

  • You have the correct Site Collection URL and Library Internal Field Name.

  • Your account has permission to modify list items in the target document library.

What the Script Does
1. Connects to SharePoint Online using PnP PowerShell.

2. Filters all PDF documents in the specified library.

3. Calculates a future "Next Review Date" (e.g., 9999-09-09) adjusted for CST or CDT.

4. Updates the metadata field (Next Review Date) for each filtered document.

PowerShell Script

$SiteUrl = "[SiteCollection]"
Connect-PnPOnline -Url $SiteUrl -UseWebLogin 

# Define the Document Library and the field name for Next Review Date
$libraryName = "[LibraryName]"  # Adjust the library name accordingly
$reviewDateField = "Next_x0020_Review_x0020_Date"  # Internal name of the field

# Get all items from the document library, including FileLeafRef
$documents = Get-PnPListItem -List $libraryName -Fields "FileLeafRef"

# Filter for PDF files only
$filteredDocuments = $documents | Where-Object { $_["FileLeafRef"] -like "*.pdf" }

# Set the new review date to a far future date
$newReviewDate = [datetime]"9999-09-09"

# Determine if the date falls under CDT or CST
$centralTimeZone = [System.TimeZoneInfo]::FindSystemTimeZoneById("Central Standard Time")
$isDST = $centralTimeZone.IsDaylightSavingTime($newReviewDate)

# Adjust for time difference from UTC to IST
if ($isDST) {
    $newReviewDate = $newReviewDate.AddHours(10).AddMinutes(30)
} else {
    $newReviewDate = $newReviewDate.AddHours(11).AddMinutes(30)
}

# Loop through filtered documents and update the field
foreach ($document in $filteredDocuments) {
    $fileLeafRef = $document["FileLeafRef"]
    Write-Host "$libraryName Document FileLeafRef: $fileLeafRef"

    Set-PnPListItem -List $libraryName -Identity $document.Id -Values @{ $reviewDateField = $newReviewDate }
}

Notes
  • Next_x0020_Review_x0020_Date is the internal name for the “Next Review Date” field. You can find this in list settings or by inspecting the field using PowerShell or the browser.
  • The script uses a placeholder date 9999-09-09 which might be used for archiving or indefinite review.
  • Adjusting time based on DST (Daylight Saving Time) ensures that timestamps align correctly across regions.
Use Cases
  • Automating document review schedules.
  • Archiving outdated or permanent documents.
  • Preparing metadata for records management.

No comments:

Post a Comment