Uploading multiple files with metadata to SharePoint Online can be time-consuming if done manually. PnP PowerShell allows bulk uploading with metadata updates efficiently.
This guide will cover:
✔ Connecting to SharePoint Online
✔ Uploading multiple files to a document library
✔ Applying metadata to the uploaded files
✔ Automating the process
Step 1: Install and Connect to SharePoint Online
Ensure PnP PowerShell is installed:
Install-Module -Name PnP.PowerShell -Force -AllowClobber
Update the module if needed:
Update-Module -Name PnP.PowerShell
Now, connect to your SharePoint site:
Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/YourSite" -Interactive
✔ Uses interactive login for authentication.
Step 2: Prepare Metadata in a CSV File
Create a CSV file (metadata.csv
) with the following format:
FileName | LibraryName | Title | Category | Department |
---|---|---|---|---|
file1.pdf | Documents | Report Q1 | Finance | HR |
file2.xlsx | Reports | Sales Data | Sales | Marketing |
file3.docx | Policies | Guidelines | Compliance | Legal |
✔ Ensure this CSV is stored in the same directory as the script.
Step 3: Bulk Upload Files and Apply Metadata
Run the following script to upload files and set metadata:
# Define CSV and local folder containing files
$csvFile = "C:\Path\To\metadata.csv"
$localFolder = "C:\Path\To\Files"
# Read metadata CSV
$files = Import-Csv -Path $csvFile
# Loop through each file and upload it with metadata
foreach ($file in $files) {
$filePath = "$localFolder\$($file.FileName)"
$library = $file.LibraryName
$serverRelativeUrl = "/sites/YourSite/$library/$($file.FileName)"
# Upload the file to the specified document library
Add-PnPFile -Path $filePath -Folder $library
# Set metadata
Set-PnPListItem -List $library -Identity $serverRelativeUrl -Values @{
"Title" = $file.Title
"Category" = $file.Category
"Department" = $file.Department
}
Write-Host "Uploaded and tagged: $($file.FileName)"
}
✔ Uploads each file to the respective document library.
✔ Applies metadata from CSV.
Step 4: Automate Bulk Uploading Using Task Scheduler
- Save the script as
BulkUpload.ps1
. - Open Task Scheduler → Create Basic Task.
- Set Trigger → Daily or Weekly.
- Set Action → Start a Program.
- Use this command:
-ExecutionPolicy Bypass -File "C:\Scripts\BulkUpload.ps1"
✔ Ensures automated bulk file uploads with metadata.
Step 5: Verify Uploaded Files & Metadata
To check uploaded files:
Get-PnPListItem -List "Documents" | Select-Object FieldValues
✔ Confirms that files are uploaded and metadata is applied.