Download Files From SharePoint Online Document Library Folder To Shared Network Drive ( File path) Using SharePoint PnP PowerShell

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.
  1. Install-Module SharePointPnPPowerShellOnline  
SharePoint Document Library Folder before Downloading files
 
Download Files From SharePoint Online Document Library Folder To Shared Network Drive ( File path) Using SharePoint PnP PowerShell
 
Network File path where Documents need to be Downloaded from SharePoint Online
 
Download Files From SharePoint Online Document Library Folder To Shared Network Drive ( File path) Using SharePoint PnP PowerShell
 
Variables Explanations in this Articles
  • $O365ServiceAccount="test@abc.com"# 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:
  1. [SecureString]$SecurePass = ConvertTo-SecureString $O365ServiceAccountPwd -AsPlainText -Force  
  2.   
  3. [System.Management.Automation.PSCredential]$PSCredentials = New-Object System.Management.Automation.PSCredential($O365ServiceAccount, $SecurePass)  
  4.   
  5. #Connecting to SharePoint Online site  
  6.   
  7. 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.
  1. $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:

  1. $Files=Get-PnPFolderItem -FolderSiteRelativeUrl $SharePointFolderPath -ItemType File  
  2. foreach($File in $Files)  
  3. {  
  4.   
  5. }  
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
  1. Get-PnPFile -Url $File.ServerRelativeUrl -Path $SharedDriveFolderPath -FileName $File.Name -AsFile  
Complete PowerShell Script
  1. #PowerShell script to Download files from SharePoint Online Document Library folder to network path  
  2. #Created By: Vinit Kumar  
  3. # Variable - Change the parameter as it need  
  4. $O365ServiceAccount = "test@abc.com"  
  5. $O365ServiceAccountPwd = "Test123$"  
  6. $SharePointSiteURL = "https://abc.sharepoint.com/sites/Test"  
  7. # Change this SharePoint Site URL  
  8. $SharedDriveFolderPath = "C:\Users\kumavini\Documents\FolderA"  
  9. # Change this Network Folder path  
  10. $SharePointFolderPath = "Shared Documents/FolderA"  
  11. # Change the Document Library and Folder path  
  12. #Ends[SecureString] $SecurePass = ConvertTo - SecureString $O365ServiceAccountPwd - AsPlainText - Force[System.Management.Automation.PSCredential] $PSCredentials = New - Object System.Management.Automation.PSCredential($O365ServiceAccount, $SecurePass)  
  13. #Connecting to SharePoint site  
  14. Connect - PnPOnline - Url $SharePointSiteURL - Credentials $PSCredentials  
  15. $Files = Get - PnPFolderItem - FolderSiteRelativeUrl $SharePointFolderPath - ItemType File  
  16. foreach($File in $Files) {  
  17.     Get - PnPFile - Url $File.ServerRelativeUrl - Path $SharedDriveFolderPath - FileName $File.Name - AsFile  
  18. }   
Shared Network path Folder Output
 
Download Files From SharePoint Online Document Library Folder To Shared Network Drive ( File path) Using SharePoint PnP PowerShell
 
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.