Upload and Download a File using PnP PowerShell

In this blog, we will see how to upload a file to a document library and how to download the file to the local path.

 First, we need to connect to the site. To perform the connection, add the following lines.

$ SiteUrl = Read-Host "Provide site url"
$LocalPathForUpload = Read-Host "Provide the path from where file to be uploaded"
$LocalPathForDownload = Read-Host "Provide the path from where file to be uploaded"
$DocumentLibrary = Read-Host "Provide the library name for upload or download file"
$Credentials = Get-Credential // It will open a pop up window and ask for credentials
Connect-PnPOnline -Url $SiteUrl -Credentials $Credentials

To Upload a file, add the following codes

$File = Add-PnPFile -Path $LocalPathForUpload -Folder $DocumentLibrary
Write-Host "Title : " $File.Name
Write-Host "URL : " $File.ServerRelativeUrl

To download the same file, add the following codes

$filePath= $ DocumentLibrary + "/test.docx"
Get-PnPFile -Url $filePath -Path $ LocalPathForDownload -Filename “test.docx " -AsFile
Write-Host "File Downloaded Successfully"

Output-

From the below images we can see our code run correctly and file uploaded and downloaded successfully.

Img1: PowerShell code runs successfully for upload and downloads a file.

Img2: File uploaded to SharePoint document library.

Img3: File downloaded to given local path

This solution is brought to you by our SharePoint professionals.

Softree Technology employs SharePoint consultants; we are a technology services provider with the aim to help companies achieve exceptional performance through SharePoint. Our dedicated team of SharePoint consultants has the right bent of mind to understand and execute customer requirements.

Be it SPFx or SharePoint add-in developments, SharePoint 2019 developments, web part developments, migrating from SharePoint 2010/2013 to SharePoint 2013/2016/Office 365, Office 365, SharePoint hosted apps development or something else in SharePoint, we strive to deliver the best

Tags: , , , ,

Leave a Reply

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