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

Uploading Files to a Document Library using PnP PowerShell

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

Loading

Uploading files to a SharePoint Online Document Library is a common requirement for automating document management. Using PnP PowerShell, you can upload files individually or in bulk, set metadata, and manage file versions efficiently.

What You’ll Learn:

✔️ How to connect to SharePoint Online
✔️ How to upload a single file
✔️ How to upload multiple files
✔️ How to set metadata for uploaded files
✔️ How to check if a file exists before uploading
✔️ How to handle errors and versioning


Prerequisites

Before proceeding, ensure that:
PnP PowerShell is installed
You have the necessary SharePoint permissions
You have the SharePoint site URL
You have a local file or folder to upload


Step 1: Install and Import PnP PowerShell

If you haven’t installed PnP PowerShell, install it using:

Install-Module -Name PnP.PowerShell -Scope CurrentUser -AllowClobber -Force

Then, import the module:

Import-Module PnP.PowerShell

PnP PowerShell is ready!


Step 2: Connect to SharePoint Online

Before uploading files, connect to your SharePoint site:

# Connect to SharePoint Online
Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/yoursite" -Interactive

🔹 Replace "yourtenant" with your tenant name
🔹 Replace "yoursite" with the site name

Connected successfully!


Step 3: Upload a Single File to a Document Library

To upload a single file to a specific document library, use the following script:

# Define variables
$libraryName = "Project Documents"
$localFilePath = "C:\Users\YourName\Documents\report.pdf"
$serverRelativePath = "/sites/yoursite/Shared Documents/report.pdf"

# Upload the file
Add-PnPFile -Path $localFilePath -Folder $libraryName

Write-Host "File uploaded successfully to $libraryName"

Replace:

  • "Project Documents" with your document library name
  • "C:\Users\YourName\Documents\report.pdf" with the file path
  • "Shared Documents" with the actual library folder

Single file uploaded successfully!


Step 4: Upload Multiple Files to a Document Library

To upload all files from a folder, use:

# Define variables
$libraryName = "Project Documents"
$localFolderPath = "C:\Users\YourName\Documents\UploadFolder"

# Upload all files in the folder
Get-ChildItem -Path $localFolderPath -File | ForEach-Object {
Add-PnPFile -Path $_.FullName -Folder $libraryName
Write-Host "Uploaded: $($_.Name)"
}

This script loops through all files in the specified folder and uploads them.

Multiple files uploaded successfully!


Step 5: Set Metadata for Uploaded Files

After uploading, you can update metadata such as Title, Category, or Custom Columns:

# Define metadata values
$libraryName = "Project Documents"
$fileName = "report.pdf"
$metadata = @{
"Title" = "Annual Report"
"Category" = "Finance"
}

# Update metadata
Set-PnPListItem -List $libraryName -Identity $fileName -Values $metadata

Write-Host "Metadata updated for $fileName"

Replace:

  • "Title" and "Category" with your SharePoint column names
  • "Annual Report" and "Finance" with metadata values

Metadata updated successfully!


Step 6: Check If a File Exists Before Uploading

To avoid overwriting existing files, check if a file exists:

# Define variables
$libraryName = "Project Documents"
$filePath = "C:\Users\YourName\Documents\report.pdf"
$fileName = "report.pdf"

# Check if file exists
$fileExists = Get-PnPListItem -List $libraryName | Where-Object { $_.FieldValues.FileLeafRef -eq $fileName }

if ($fileExists) {
Write-Host "File '$fileName' already exists. Skipping upload."
} else {
Add-PnPFile -Path $filePath -Folder $libraryName
Write-Host "File uploaded successfully."
}

File existence check implemented!


Step 7: Handle Versioning While Uploading

If versioning is enabled, you can overwrite existing files:

# Upload and overwrite existing file
Add-PnPFile -Path "C:\Users\YourName\Documents\report.pdf" -Folder "Project Documents" -Overwrite

🔹 If a file already exists, this will replace it with the new version.

Versioning handled successfully!


Step 8: Verify Uploaded Files in SharePoint

To list all files in the document library, use:

# Get all files in the library
Get-PnPListItem -List "Project Documents" | Select-Object FieldValues.FileLeafRef

File verification successful!


Step 9: Delete an Uploaded File (If Needed)

If you need to remove a file, use:

# Define variables
$libraryName = "Project Documents"
$fileName = "report.pdf"

# Remove file
Remove-PnPFile -ServerRelativeUrl "/sites/yoursite/Shared Documents/$fileName" -Force

Write-Host "File '$fileName' deleted successfully!"

File deleted successfully!


Common Errors & Solutions

ErrorCauseSolution
Access DeniedInsufficient permissionsEnsure you have Contribute or Full Control rights
Cannot find libraryIncorrect library nameUse Get-PnPList to verify the name
File already existsUploading a duplicate fileUse -Overwrite to replace existing files
Posted Under PNP PowerShellBulk Upload Document library File Automation Metadata PNP PowerShell SharePoint online Upload Files versioning

Post navigation

EntityFrameworkCore.DbUpdateConcurrencyException – Database operation expected to affect n rows
IoT-Based Fire Detection and Safety Systems

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