In this blog, I will demonstrate how to download all files from a SharePoint Document Library folder to a specified Shared path using SharePoint PnP PowerShell Script. This is useful for a scenario where an automated script is needed to download all files from a SharePoint Library folder to a Specific network path for processing like Expense Microsoft Excel files.
Pre-requisites( Environment Details)
- Windows PowerShell
- SharePointPnpPowerShellOnline Module
Please install the SharePointPnpPowerShellOnline module if it’s not already present using the below command.
- Install-Module SharePointPnPPowerShellOnline
SharePoint Document Library Folder before Downloading files

Network File path where Documents need to be Downloaded from SharePoint Online

Variables Explanations in this Articles
- $O365ServiceAccount="[email protected]"# Your Service Account Name
- $O365ServiceAccountPwd="Test123$"#Your Service Account Password
- $SharePointSiteURL="https://abc.sharepoint.com/sites/Test" # Change this SharePoint Site URL
- $SharedDriveFolderPath=" C:\Users\kumavini\Documents\FolderA" # Change this Network Folder path
- $SharePointFolderPath="Shared Documents/FolderA" # Change the Document Library and Folder path
Here you can see we have provided the password in plain text which you need to provide if you want this PowerShell script to run automatically through Timer Job or Scheduler.
For manual execution please use the Get-Credential command to read the user name and password from the user input.
Connect to SharePoint Online
Before downloading the files to SharePoint Online you should be connecting to the SharePoint Site using the below snippet:
- [SecureString]$SecurePass = ConvertTo-SecureString $O365ServiceAccountPwd -AsPlainText -Force
- [System.Management.Automation.PSCredential]$PSCredentials = New-Object System.Management.Automation.PSCredential($O365ServiceAccount, $SecurePass)
- #Connecting to SharePoint Online site
- Connect-PnPOnline -Url $SharePointSiteURL -Credentials $PSCredentials
Read Files from the SharePoint Document Library Folder
We have to read all files from the SharePoint Document Library Folder using the Get-PnPFolderItem command of PowerShell as in the code snippet below.
- $Files=Get-PnPFolderItem -FolderSiteRelativeUrl $SharePointFolderPath -ItemType File
The above code only read files within the SharePoint Document Library Folder, not any sub Folder when we used property -ItemType File and stored into the $Files variable.
Now apply For Each to read all the files one by one in the below snippet:
- $Files=Get-PnPFolderItem -FolderSiteRelativeUrl $SharePointFolderPath -ItemType File
- foreach($File in $Files)
- {
- }
Download Files to Network Shared Path
SharePointPnpPowerShellOnline Module of PowerShell has made developers' lives easier. To download a file from a SharePoint Document Library to a specific shared location, use the code snippet as below.
Please find all the parameters associated with Get-PnPFile
- Get-PnPFile -Url $File.ServerRelativeUrl -Path $SharedDriveFolderPath -FileName $File.Name -AsFile
Complete PowerShell Script
- #PowerShell script to Download files from SharePoint Online Document Library folder to network path
- #Created By: Vinit Kumar
- # Variable - Change the parameter as it need
- $O365ServiceAccount = "[email protected]"
- $O365ServiceAccountPwd = "Test123$"
- $SharePointSiteURL = "https://abc.sharepoint.com/sites/Test"
- # Change this SharePoint Site URL
- $SharedDriveFolderPath = "C:\Users\kumavini\Documents\FolderA"
- # Change this Network Folder path
- $SharePointFolderPath = "Shared Documents/FolderA"
- # Change the Document Library and Folder path
- #Ends[SecureString] $SecurePass = ConvertTo - SecureString $O365ServiceAccountPwd - AsPlainText - Force[System.Management.Automation.PSCredential] $PSCredentials = New - Object System.Management.Automation.PSCredential($O365ServiceAccount, $SecurePass)
- #Connecting to SharePoint site
- Connect - PnPOnline - Url $SharePointSiteURL - Credentials $PSCredentials
- $Files = Get - PnPFolderItem - FolderSiteRelativeUrl $SharePointFolderPath - ItemType File
- foreach($File in $Files) {
- Get - PnPFile - Url $File.ServerRelativeUrl - Path $SharedDriveFolderPath - FileName $File.Name - AsFile
- }
Shared Network path Folder Output

In this blog, we have talked about how to download all files from a SharePoint Online Document Library Folder to Network shared drive directory using SharePoint Online PnP PowerShell script. In my previous blog I have written how to copy all files from a network drive to network shared drive directory using SharePoint Online PnP PowerShell Script.

Tech BradPosted Feb 28, 2023, 6:48 PM
Well done! This is precisely what I was looking for because COPY-PNPFILE & COPY-PNPFOLDER seem to only work for SharePoint2SharePoint. SharePoint2LocalFilePath copy you have to jump through hoops like you've displayed.. (If you pass in a localfilepath variable into -targetURL parameter incorrectly appends SharePoint site connection info /sites/DocumentLibrary/ into the beginning of the parameter.
Daniel KPosted Nov 18, 2022, 4:02 PM
Vinit Kumar Could you please let me know if you have any PnP PowerShell script to export SharePoint online list items to Network Path location .Thank you
Meelie yingPosted Jan 5, 2022, 3:17 PM
I can download files van sharepoint. thanks
Reuben GuptaPosted May 3, 2021, 1:46 AM
Move-PnPFile -SourceUrl "https://mulpha.sharepoint.com/:w:/r/sites/Aus-Hub/MulphaIT/Shared%20Documents/Test%20file%20for%20FELIX%20project.docx?d=w3b4122e246314a689530136ee10e03b8&csf=1&web=1&e=h2B3ah" -TargetUrl "C:\Users\Reuben.Gupta\OneDrive - Mulpha International Bhd\Desktop\Test SQL Felix" Confirm Move file '/https://mulpha.sharepoint.com/:w:/r/sites/Aus-Hub/MulphaIT/Shared%20Documents/Test%20file%20for%20FELIX%20project.docx?d=w3b4122e246314a689530136ee10e03b8&csf=1&web=1&e=h2B3ah' to '/C:/Users/Reuben.Gupta/OneDrive - Mulpha International Bhd/Desktop/Test SQL Felix'? [Y] Yes [N] No [S] Suspend [?] Help (default is "Y"): Y Move-PnPFile : The file or folder name "C:\Users\Reuben.Gupta\OneDrive - Mulpha International Bhd\Desktop\Test SQL Felix" contains invalid characters. Please use a different name. Common invalid characters include the following: # % * : < > ? / | At line:1 char:1 + Move-PnPFile -SourceUrl "https://mulpha.sharepoint.com/:w:/r/sites/Au ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (:) [Move-PnPFile], ServerException + FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.MoveFile
Reuben GuptaPosted May 3, 2021, 1:45 AM
Hi - I am trying to paste a file from sharepoint to local drive folder - pls can you help with the error -------------
Jeffrey WestPosted Dec 8, 2020, 5:27 PM
Is there a way to automate without having plain text password? I'd like to do this with a service account in AD and run the script within a scheduled task. Thoughts?
Hitesh LadePosted Nov 27, 2020, 3:08 PM
That worked.. Thanks Vinit
Olegs BazalickisPosted Oct 1, 2020, 9:02 AM
Is it possible to copy not only files but also folders with all contents?