Skip to content
Rishan Solutions
Rishan Solutions
  • PowerApps
  • SharePoint online
    • Uncategorized
    • Uncategorized
  • PowerAutomate
Rishan Solutions
Latest Posts
  • Agentic AI: The Dawn of Autonomous Intelligence Revolutionizing 2025 June 24, 2025
  • Recursive Queries in T-SQL May 7, 2025
  • Generating Test Data with CROSS JOIN May 7, 2025
  • Working with Hierarchical Data May 7, 2025
  • Using TRY_CAST vs CAST May 7, 2025
  • Dynamic SQL Execution with sp_executesql May 7, 2025

Automating Document Classification Using PnP PowerShell

Posted on March 19, 2025March 19, 2025 by Rishan Solutions

Loading

Automating document classification in SharePoint Online improves content organization, retrieval, and compliance. Using PnP PowerShell, we can:

Assign content types dynamically
Apply metadata and tags based on file properties
Set up retention labels for compliance
Move or categorize documents automatically


Step 1: Prerequisites

1.1 Install and Update PnP PowerShell

Ensure PnP PowerShell is installed:

Install-Module PnP.PowerShell -Scope CurrentUser

Update if needed:

Update-Module PnP.PowerShell

1.2 Connect to SharePoint

Authenticate with your SharePoint site:

Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/YourSite" -Interactive

Result: You are now connected to your SharePoint environment.


Step 2: Retrieve Documents from a Library

To get all files from a Document Library:

$LibraryName = "Documents"
$Files = Get-PnPListItem -List $LibraryName
$Files | Select Title, FileLeafRef, FileDirRef, ContentType

Result: Fetches document titles, names, paths, and content types.


Step 3: Assign Content Types Based on File Types

To automatically classify documents by file type:

$LibraryName = "Documents"

$Files = Get-PnPListItem -List $LibraryName

foreach ($File in $Files) {
$FileName = $File["FileLeafRef"]

if ($FileName -like "*.pdf") {
Set-PnPListItem -List $LibraryName -Identity $File.Id -Values @{"ContentTypeId"="0x010100ABC123"} # Content Type ID for "Reports"
}
elseif ($FileName -like "*.xlsx") {
Set-PnPListItem -List $LibraryName -Identity $File.Id -Values @{"ContentTypeId"="0x010100DEF456"} # Content Type ID for "Financials"
}
}

Result: Assigns content types based on document format.


Step 4: Apply Metadata Automatically

To classify files based on their names or properties, use:

foreach ($File in $Files) {
$FileName = $File["FileLeafRef"]

if ($FileName -like "*Invoice*") {
Set-PnPListItem -List $LibraryName -Identity $File.Id -Values @{"Category"="Finance"; "Department"="Accounts"}
}
elseif ($FileName -like "*Contract*") {
Set-PnPListItem -List $LibraryName -Identity $File.Id -Values @{"Category"="Legal"; "Department"="Compliance"}
}
}

Result: Assigns metadata tags based on document keywords.


Step 5: Assign Retention Labels Automatically

To enforce data compliance using retention labels:

foreach ($File in $Files) {
if ($File["FileLeafRef"] -like "*Confidential*") {
Set-PnPListItem -List $LibraryName -Identity $File.Id -Values @{"ComplianceTag"="Confidential-5Years"}
}
}

Result: Applies retention labels automatically.


Step 6: Move Documents to Folders Based on Classification

To organize documents by moving them to folders:

foreach ($File in $Files) {
$FileName = $File["FileLeafRef"]

if ($FileName -like "*HR*") {
Move-PnPFile -ServerRelativeUrl "/sites/YourSite/Shared Documents/$FileName" -TargetUrl "/sites/YourSite/Shared Documents/HR/$FileName" -Force
}
}

Result: Moves documents based on classification.


Step 7: Automate with a Scheduled Task

To schedule this classification script daily:

1️⃣ Save the script as ClassifyDocuments.ps1
2️⃣ Open Task Scheduler in Windows
3️⃣ Create a new task
4️⃣ Set the trigger to Daily
5️⃣ Set the action to Run PowerShell script:

powershell.exe -ExecutionPolicy Bypass -File "C:\Scripts\ClassifyDocuments.ps1"

Result: Automates document classification daily.

Posted Under PNP PowerShellAI-Based Classification Automation Scripts Content Types Document Classification Managed Properties Metadata PNP PowerShell Retention Labels SharePoint Automation SharePoint Document library Taxonomy

Post navigation

Future of IoT in 2030 and Beyond
Classes and Objects in Java

Leave a Reply Cancel reply

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

Recent Posts

  • Agentic AI: The Dawn of Autonomous Intelligence Revolutionizing 2025
  • Recursive Queries in T-SQL
  • Generating Test Data with CROSS JOIN
  • Working with Hierarchical Data
  • Using TRY_CAST vs CAST

Recent Comments

  1. Michael Francis on Search , Filter and Lookup in power apps
  2. A WordPress Commenter on Hello world!

Archives

  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • March 2024
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • June 2023
  • May 2023
  • April 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • January 2022

Categories

  • Active Directory
  • AI
  • AngularJS
  • Blockchain
  • Button
  • Buttons
  • Choice Column
  • Cloud
  • Cloud Computing
  • Data Science
  • Distribution List
  • DotNet
  • Dynamics365
  • Excel Desktop
  • Extended Reality (XR) – AR, VR, MR
  • Gallery
  • Icons
  • IoT
  • Java
  • Java Script
  • jQuery
  • Microsoft Teams
  • ML
  • MS Excel
  • MS Office 365
  • MS Word
  • Office 365
  • Outlook
  • PDF File
  • PNP PowerShell
  • Power BI
  • Power Pages
  • Power Platform
  • Power Virtual Agent
  • PowerApps
  • PowerAutomate
  • PowerPoint Desktop
  • PVA
  • Python
  • Quantum Computing
  • Radio button
  • ReactJS
  • Security Groups
  • SharePoint Document library
  • SharePoint online
  • SharePoint onpremise
  • SQL
  • SQL Server
  • Template
  • Uncategorized
  • Variable
  • Visio
  • Visual Studio code
  • Windows
© Rishan Solutions 2025 | Designed by PixaHive.com.
  • Rishan Solutions