Download All Files From A SharePoint Online Document Library Using PnP PowerShell

One of my clients recently asked us to write a PowerShell script to download all files from a SharePoint Online document library. SharePoint lets you download individual or multiple files at once by clicking the Download option. We can also perform the same operation using PnP PowerShell.

In this article, I will explain how to download all files from a SharePoint Online document library using PnP PowerShell.

Download All Files In SharePoint Document Library Using PnP PowerShell

In the example below, I have a SharePoint document library named “Employee Travel Authorization PDF Files,” which contains several files.

download multiple files from sharepoint using powershell

After running the Pnp PowerShell script, it downloads each file present in the document library into my local system.

how to download all files in SharePoint document library using PnP PowerShell

Now, let’s understand how this PnP PowerShell script downloads all the files from the SharePoint document library.

Note: This script will also iterate over each folder in the SharePoint document library and download the files within it.

$SiteURL = "https://<tenant name>.sharepoint.com/sites/TravelAuthorizationSite"
$LibraryName = "Employee Travel Authorization PDF Files"
$LocalFolderPath = "D:\NewFolder
$ClientID = "Your client id"

Connect-PnPOnline -Url $SiteURL -ClientId $ClientID -Interactive

# Check the local folder exists
if (-not (Test-Path -Path $LocalFolderPath)) {
    New-Item -Path $LocalFolderPath -ItemType Directory | Out-Null
}
$Files = Get-PnPListItem -List $LibraryName -PageSize 500 | Where-Object { $_.FileSystemObjectType -eq "File" }

# Loop through each file and download it
foreach ($Item in $Files) {
    $FileRefUrl = $Item.FieldValues["FileRef"]           
    $FileName = $Item.FieldValues["FileLeafRef"]   
    $TargetFilePath = Join-Path $LocalFolderPath $FileName

    Write-Host "Downloading: $FileName"
    Get-PnPFile -Url $FileRefUrl -Path $LocalFolderPath -FileName $FileName -AsFile -Force
}

Here,

  • Provide the input to the given parameters at the start of the script, such as:
    • $SiteURL
    • $LibraryName
    • $LocalFolderPath
    • $ClientID
  • Connect-PnPOnline = Used to connect with the SharePoint site.
  • if (-not (Test-Path -Path $LocalFolderPath)) = In the if condition, the Test-Path will determine whether all elements of a path exist.
  • If not, the New-Item command will create a directory item in the provided folder.
  • Using the Get-PnPListItem cmdlet, we fetch only files from the given library. The page size is 500, and we apply filters to fetch only files, which are stored in this $Files array.
  • Using a foreach loop, we’re iterating over each file and fetching its properties like
    • Server-relative URL
    • File name
  • $TargetFilePath = Stores the path which is combination of $LocalFolderPath $FileName.
  • Get-PnPFile = It allows you to download a file from SharePoint Online, and it takes parameters like:
    • -Url = Needs to provide server relative URL path of the file [$FileRefUrl]
    • -Path = Provide path where to download [$LocalFolderPath]
    • -FileName = Provide each file name [$FileName]
    • -AsFile = Save to local path.
    • -Force = Overwrites the file if it exists.

This way, we can easily download all files from the SharePoint document library.

Download Multiple Specified Files From SharePoint Using PnP PowerShell

Previously, we saw how to download all files from SharePoint Online. Now, I will explain how to download multiple specific files using PnP PowerShell, as shown below.

download multiple files from SharePoint using PnP PowerShell

Run the below script in VS Code by updating the variable values with your own.

$siteUrl = "https://<tenant name>.sharepoint.com/sites/TravelAuthorization"
$ClientID = "Your Client ID"
$destinationPath = "D:\ListColumns" 

$files = "/sites/TravelAuthorization/EmployeeTravelAuthorizationPDFFiles/Folder1/Patti FernandezTravel-Req 441173.pdf", "/sites/TravelAuthorization/EmployeeTravelAuthorizationPDFFiles/Folder3/MiriamTravel-Req 949154.pdf"  

Connect-PnPOnline -Url $SiteURL -ClientId $ClientID -Interactive

$files | ForEach-Object {  
    $splitpath = $_.Split("/")          
    $splitpathCount = $splitpath.Length   
    $fileName = $splitpath[$splitpathCount - 1]  
    Get-PnPFile -Url $_ -Path $destinationPath -FileName $fileName -AsFile  
}

Here,

  • $files = Provide your file paths in this variable. Its format should be as follows, also separating multiple file paths with a comma (,).
    • “/sites/sitename/LibraryName/FolderName/Filename.pdf”
  • ForEach-Object will iterate over each object in the $files variable and download it to the specified local path.

This way, we can download multiple specified files from SharePoint Online.

Download Specific File Types From SharePoint Document Library

Here, we will learn how to download specific files from a SharePoint document library, such as .xlsx and .docx files.

Download Multiple Documents From SharePoint Using PnP PowerShell

In this PowerShell script, I just filtered the file types with .xlsx and .docx. In case you want to include .pdf files or any other type, just update the file types in the $allowedExtensions variable in the below script.

$SiteURL = "https://<tenant name>.sharepoint.com/sites/TravelAuthorization"
$LibraryName = "Employee Travel Authorization PDF Files"
$LocalFolderPath = "D:\ListColumns"
$ClientID = "Your Clinet ID"
$allowedExtensions = ".xlsx", ".docx"

Connect-PnPOnline -Url $SiteURL -ClientId $ClientID -Interactive

# Check the local folder exists
if (-not (Test-Path -Path $LocalFolderPath)) {
    New-Item -Path $LocalFolderPath -ItemType Directory | Out-Null
}

# Get all files in the document library
$Files = Get-PnPListItem -List $LibraryName -PageSize 500 | Where-Object { $_.FileSystemObjectType -eq "File" }

# Loop through each file and download it if the extension is allowed
foreach ($Item in $Files) {
    $FileRefUrl = $Item.FieldValues["FileRef"]           
    $FileName = $Item.FieldValues["FileLeafRef"]    
    $Extension = [System.IO.Path]::GetExtension($FileName).ToLower()

    if ($allowedExtensions -contains $Extension) {
        $TargetFilePath = Join-Path $LocalFolderPath $FileName
        Write-Host "Downloading: $FileName"
        Get-PnPFile -Url $FileRefUrl -Path $LocalFolderPath -FileName $FileName -AsFile -Force
    } else {
        Write-Host "Skipping unsupported file: $FileName"
    }
}

As we discussed above, this script is similar to downloading all files from the SharePoint library. However, we are adding an if condition to download only a specific type of file.

  • $allowedExtensions = This variable contains the file types.
  • $Extension = This variable in the foreach loop stores each file type.
  • Before downloading the files, we’re checking whether the file extension is present in the allowed extension variable. If yes, downloading; if not, skipping downloading files.
download specific file types from sharepoint using powershell

In the Terminal pane, you can see that files with the .pdf extension are skipping, while the remaining files are downloading.

Powershell Script to Download a File From Specific Folders in Sharepoint

So far, we have seen downloading multiple files under specific conditions. Let’s see how to download a specific file from a particular folder in the library using PnP PowerShell.

The example below shows that the marked file in the document library is downloaded into my local system.

Powershell script to download specific file from Sharepoint
$siteUrl = "https://<tenant name>.sharepoint.com/sites/TravelAuthorizationSite"
$libraryName = "Employee Travel Authorization PDF Files"  
$folderPath = "TravelAuthorizationPDFFiles/Folder1"
$fileName = "Patti Fernandez-Travel-Req 168378.pdf"
$destinationPath = "D:\ListColumns\Patti Fernandez-Travel-Req 168378.pdf"
$ClientID = "Your Client ID"

# Connect to SharePoint Online
Connect-PnPOnline -Url $SiteURL -ClientId $ClientID -Interactive

# Get the file
$fileUrl = "/sites/TravelAuthorization/$folderPath/$fileName"

# Download the file
Get-PnPFile -Url $fileUrl -Path (Split-Path -Path $destinationPath -Parent) -FileName $fileName -AsFile -Force

Here,

  • $fileName = Provide the file name that you want to download.
  • $folderPath = Provide the folder path where the file is present.
  • $destinationPath = Provide the destination path where the file needs to be downloaded.
  • $fileUrl = In this variable, update your site name in the path.
    • “/sites/your site name/$folderPath/$fileName”

Following the script, we can download a specific file from the SharePoint document library.

In this article, I explain how to download all files, multiple specified files, files of a specified type, and a single file from a SharePoint document library using PnP PowerShell.

I hope you found this article helpful! Do let me know in the comments below if you face any issues.

Also, you may like:

Power Apps functions free pdf

30 Power Apps Functions

This free guide walks you through the 30 most-used Power Apps functions with real business examples, exact syntax, and results you can see.

Live Webinar

Quiz App Using SharePoint Framework (SPFx)

Learn to built a complete Quiz Management solution that enables admins to create and manage quizzes, categories, questions, and settings with an easy automated setup process in SharePoint. It also includes an interactive quiz experience for users and a powerful dashboard to track participation, analyze results, and view detailed performance reports with charts and answer insights.

📅 2nd June 2026 – 10:00 AM EST | 7:30 PM IST

Download User registration canvas app

DOWNLOAD USER REGISTRATION POWER APPS CANVAS APP

Download a fully functional Power Apps Canvas App (with Power Automate): User Registration App