How to Download Files from SharePoint Document Library Using PnP PowerShell?

Recently, I got a requirement to download a file from a document library in SharePoint. For this, I use PnP PowerShell. I will show you how to download files from a SharePoint document library using PnP PowerShell in this tutorial.

I will show you how to download a single file from a SharePoint Online document library using PnP PowerShell. Also, I will show you how to download multiple files from a SharePoint document library using PnP PowerShell.

Download a File from SharePoint Document Library Using PnP PowerShell

To download a single file from a SharePoint document library using PnP PowerShell, you will need to specify the path to the file in the library and the local path where you want to save the file. Here’s the complete code:

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

$libraryName = "HRDocuments"
$fileName = "MyWordFile.docx"
$localPath = "C:\Downloads\"

# Get the file
$file = Get-PnPFile -Url "/$libraryName/$fileName" -Path $localPath -FileName $fileName -AsFile

Write-Host "File downloaded to $localPath"

Once you execute the above PowerShell script, it downloads the document from the SharePoint Online document library and saves it in my local folder.

Download a File from SharePoint Document Library Using PnP PowerShell

Read Download File from URL in PowerShell

Download Multiple Files from SharePoint Document Library Using PnP PowerShell

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

Below is the PnP PowerShell script to download all the files from the document library in SharePoint Online.

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

# Define the library and local download path
$libraryName = "HRDocuments"
$localFolderPath = "C:\Downloads\"

# Ensure the local folder exists
if (-Not (Test-Path -Path $localFolderPath)) {
    New-Item -ItemType Directory -Path $localFolderPath
}

# Get all files from the library
$files = Get-PnPListItem -List $libraryName | Where-Object { $_.FileSystemObjectType -eq "File" }

foreach ($file in $files) {
    $fileUrl = $file.FieldValues.FileRef
    $fileName = $file.FieldValues.FileLeafRef
    $localFilePath = Join-Path -Path $localFolderPath -ChildPath $fileName

    # Download the file
    Get-PnPFile -Url $fileUrl -Path $localFolderPath -FileName $fileName -AsFile

    Write-Host "File $fileName downloaded to $localFolderPath"
}

Once you execute the above PowerShell script, it will download all the files to the local folder. You can see the output in the screenshot below, after I executed the above PowerShell script.

Download a File from SharePoint Document Library Using PnP PowerShell

If you want to download files in bulk from a SharePoint Online document library using PnP PowerShell, then I hope the above complete script will be helpful.

Conclusion

In this tutorial, I have explained how to download a single file from a SharePoint Online document library using PnP PowerShell and how to download multiple files from a document library using PnP PowerShell in SharePoint Online.

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.