SCENARIO
You have an automated process that creates a large quantity of documents on a local or network drive and you need a quick, easy and cheap way to upload those documents to a SharePoint Online site. PowerShell PnP is the answer.
PREREQUISITES
Before running the script, you’ll need the following:
- PnP.PowerShell installed – At the time of writing, I’m using 2.12.0
- Site Collection Admin on the site you want to upload to
- PowerShell 7
POWERSHELL SCRIPT
This script will take the contents of a local folder, loop through each of the files and upload them to the root folder of the document library on the target SharePoint site.
# Connect to SharePoint Online $SiteURL = "https://yourtenant.sharepoint.com/sites/yoursite" $clientID = "yourclientIDGUID" $libraryName = "LibraryName" $LocalFolderPath = "C:\FilesToUpload\" Connect-PnPOnline -Url $siteUrl -Interactive -ClientId $clientID # Upload files from the local folder $Files = Get-ChildItem -Path $LocalFolderPath foreach ($File in $Files) { $FilePath = $File.FullName $TargetURL = "$LibraryName/$($File.Name)" Write-Host "Uploading $($File.Name) to $LibraryName..." Add-PnPFile -Path $FilePath -Folder $LibraryName } Write-Host "Upload complete!"