How to Upload Files to a SharePoint Document Library using PnP PowerShell?

Recently, one of my clients asked me to upload some files from their local drive to a SharePoint Online document library. I tried PnP PowerShell, and that worked great. In this tutorial, I will explain how to upload files to a SharePoint document library using PnP PowerShell. We will cover uploading a single file as well as multiple files to a SharePoint library.

Here, I have created a document library in my SharePoint Online site, and I have a local folder with some files in it. We will upload these local files to the SharePoint library.

Upload file to SharePoint library
Upload file to SharePoint using PnP PowerShell

Upload a Single File to SharePoint Online using PnP PowerShell

Now, first, let me show you how to upload a single file to a SharePoint Online document library using PnP PowerShell.

Here is the complete PowerShell script.

$siteUrl = "https://szg52.sharepoint.com/sites/PowerShellFAQs"
Connect-PnPOnline -Url $siteUrl -Interactive

# Upload the file
Add-PnPFile -Path "C:\MyNewFolder\MyWordFile.docx" -Folder "HRDocuments"

Once you execute the above PowerShell script, it will upload the file to the SharePoint document library. I took the screenshot below after I executed the script.

$siteUrl = "https://szg52.sharepoint.com/sites/PowerShellFAQs" Connect-PnPOnline -Url $siteUrl -Interactive # Upload the file Add-PnPFile -Path "C:\MyNewFolder\MyWordFile.docx" -Folder "HRDocuments"

This is how easily you can upload a single file to a SharePoint library using PnP PowerShell.

While uploading documents, sometimes you might need to update column values, and for that, you can write the PnP PowerShell script like the below:

$siteUrl = "https://szg52.sharepoint.com/sites/PowerShellFAQs"
Connect-PnPOnline -Url $siteUrl -Interactive

# Upload the file
Add-PnPFile -Path "C:\MyNewFolder\MyWordFile.docx" -Folder "HRDocuments"
Set-PnPListItem -List "HRDocuments" -Identity $file.ListItemAllFields.Id -Values @{"Column1"="Value1"; "Column2"="Value2"; "Column3"="Value3"}

Read Download Files from SharePoint Document Library Using PnP PowerShell

Upload Multiple Files to SharePoint using PnP PowerShell

Now, let me show you how to upload multiple files to a SharePoint library using PnP PowerShell.

In this case, as I said before I have a few files inside my local folder.

$siteUrl = "https://szg52.sharepoint.com/sites/PowerShellFAQs"
Connect-PnPOnline -Url $siteUrl -Interactive

$folderPath = "C:\MyNewFolder"
$files = Get-ChildItem -Path $folderPath

foreach ($file in $files) {
    Add-PnPFile -Path $file.FullName -Folder "HRDocuments"
}

Once you execute the above script, you can see it will upload all the files from the local drive to the SharePoint library. Check the screenshot below:

Upload Multiple Files to SharePoint using PnP PowerShell

Also, if you want to update column values while uploading documents to the SharePoint document library, you can write the below PnP PowerShell script.

$siteUrl = "https://szg52.sharepoint.com/sites/PowerShellFAQs"
Connect-PnPOnline -Url $siteUrl -Interactive

$folderPath = "C:\MyNewFolder"
$files = Get-ChildItem -Path $folderPath

foreach ($file in $files) {
    Add-PnPFile -Path $file.FullName -Folder "HRDocuments"
    Set-PnPListItem -List "HRDocuments" -Identity $uploadedFile.ListItemAllFields.Id -Values @{"Column1"="Value1"; "Column2"="Value2"; "Column3"="Value3"}
}

Conclusion

In this tutorial, I have explained how to upload documents to a SharePoint document library using PnP PowerShell. I have explained how to upload a single document to SharePoint using PnP PowerShell. And also, I have explained about bulk upload files to SharePoint using PnP PowerShell.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.