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
- 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.