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.
  1. $ SiteUrl = Read-Host "Provide site url"  
  2. $LocalPathForUpload = Read-Host "Provide the path from where file to be uploaded"  
  3. $LocalPathForDownload = Read-Host "Provide the path from where file to be uploaded"  
  4. $DocumentLibrary = Read-Host "Provide the library name for upload or download file"  
  5. $Credentials = Get-Credential // It will open a pop up window and ask for credentials  
  6. Connect-PnPOnline -Url $SiteUrl -Credentials $Credentials  
To Upload a file, add the following codes,
  1. $File = Add-PnPFile -Path $LocalPathForUpload -Folder $DocumentLibrary  
  2. Write-Host "Title : " $File.Name  
  3. Write-Host "URL : " $File.ServerRelativeUrl   
To download the same file, add the following codes,
  1. $filePath= $ DocumentLibrary + "/test.docx"  
  2. Get-PnPFile -Url $filePath -Path $ LocalPathForDownload -Filename “test.docx " -AsFile  
  3. Write-Host "File Downloaded Successfully"  
Output
 
From the below images we can see our code run correctly and file uploaded and downloaded successfully.
 
Upload And Download A File Using PnP PowerShell
Img1: PowerShell code runs successfully for upload and downloads a file. 
 
Upload And Download A File Using PnP PowerShell
Img2: File uploaded to SharePoint document library. 
 
Upload And Download A File Using PnP PowerShell
Img3: File downloaded to given local path