Upload Document To SharePoint Library Using PnP PowerShell

This blog explains how to upload the document from local machine to SharePoint library using PnP PowerShell.
 
Syntax
  1. Add-PnPFile -Path <String>  
  2.             -Folder <String>  
  3.             [-Checkout [<SwitchParameter>]]  
  4.             [-CheckInComment <String>]  
  5.             [-Approve [<SwitchParameter>]]  
  6.             [-ApproveComment <String>]  
  7.             [-Publish [<SwitchParameter>]]  
  8.             [-PublishComment <String>]  
  9.             [-UseWebDav [<SwitchParameter>]]  
  10.             [-Values <Hashtable>]  
  11.             [-ContentType <ContentTypePipeBind>]  
  12.             [-Web <WebPipeBind>]  
  13.             [-Connection <SPOnlineConnection>]  
Note

The syntax may change in future.
 
Reference

https://github.com/SharePoint/PnP-PowerShell/blob/master/Documentation/Add-PnPFile.md
 
By using Add-PnPFile, we can upload the document and stream to SharePoint Library. And, this command also allows us to modify the property for the uploaded document.
 
The below snippet is used to upload the local file specified in Local FilePath to the folder in SharePoint library. 
  1. $cred = Get-Credential  
  2. Connect-PnPOnline -Url https://tenant.sharepoint.com/sites/dev -Credential $cred  
  3. $localpath = "<Local FilePath>"  
  4. $splib = "<FolderName>"  
  5. Add-PnPFile -Path $localPath -Folder $splib  
Upload File to Library's Root Folder

The screenshot gets the Testdocument.docx file from the local machine under "E:\ktskumar\files\" folder and uploads that to SharePoint library called "Demo Documents"; its folder URL is DemoDocuments.



Output

The output for the above PowerShell command is in the screenshot below. From the below output, you can see that the document is uploaded to the Demo Documents library.



Upload File to subfolder in a Library

The following screenshot uploads the file TestDocument.docx from "E:\ktskumar\files" and uploads to the test files under Demo Document library. Even though the library doesn't have the folder, the PowerShell command creates the folder and uploads the file.

 
Output

The below output shows the uploaded document under the testfiles folder within Demo Documents library.