SharePoint 2016/2013/Online: How To Download SharePoint Files By Powershell Automation
In this article, we will explore the PowerShell way of downloading the files in SharePoint Document Libraries.
By adding some flavor of automation mechanism this technique could be very useful to the scenarios where we need to download documents in bulk in a schedule based job.
In order to set up this demo, we have created a document library in my SharePoint Online Site, though this equally applicable to On-Premise implementations of SharePoint.
We have added a new document library in SharePoint Online Site by the name “Prashant Documents” with a set of documents uploaded to it.
https://howtodowithsharepoint.files.wordpress.com/2016/09/0-01.png?w=800
We have also set up a local folder on the disk drive which will act as the destination folder for the files to be downloaded.
https://howtodowithsharepoint.files.wordpress.com/2016/09/0-11.png?w=800
Step 1: Initiate the Client Context.
In order to get more information on Setting up Client Context & other Environmental Configurations, you may visit one of the earlier articles SharePoint Online: How to Install SharePoint Online Management Shell
https://howtodowithsharepoint.files.wordpress.com/2016/09/11.png?w=800
Step 2: Instantiate & Load SharePoint Document Library Object.
Step 3: Load the Root Folder in SharePoint Document Library which is containing the set of documents to be downloaded.
Step 4: Using the “OpenBinaryDirect” Method that is available in “Microsoft.SharePoint.Client.File” Class to read the file from SharePoint Document Library and save the File Stream to the disk location by using “CopyTo” method which is available in “System.IO.File” Class.
https://howtodowithsharepoint.files.wordpress.com/2016/09/21.png?w=748&h=340
Step 5: Call “DownloadFilesFromFolder” function that will download the files to the disk location.
https://howtodowithsharepoint.files.wordpress.com/2016/09/31.png?w=800
Step 6: On execution of this function Windows PowerShell Credential Prompt will appear where we have to specify the password to the login account.
https://howtodowithsharepoint.files.wordpress.com/2016/09/41.png?w=800
Step 7: Once the script executed successfully we can see the documents downloaded into the disk location.
https://howtodowithsharepoint.files.wordpress.com/2016/09/51.png?w=800
Hope you find it helpful.