This blog explains how to upload the document from local machine to SharePoint library using PnP PowerShell.
Syntax
- Add-PnPFile -Path <String>
- -Folder <String>
- [-Checkout [<SwitchParameter>]]
- [-CheckInComment <String>]
- [-Approve [<SwitchParameter>]]
- [-ApproveComment <String>]
- [-Publish [<SwitchParameter>]]
- [-PublishComment <String>]
- [-UseWebDav [<SwitchParameter>]]
- [-Values <Hashtable>]
- [-ContentType <ContentTypePipeBind>]
- [-Web <WebPipeBind>]
- [-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.
- $cred = Get-Credential
- Connect-PnPOnline -Url https:
- $localpath = "<Local FilePath>"
- $splib = "<FolderName>"
- 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.