SharePoint 2016/2013/Online - How To Download SharePoint Files By PowerShell Automation

By adding some flavor with an automation mechanism, this technique could be very useful to the scenarios wherein we need to download documents in bulk on a schedule based job.

In order to setup this demo, I have created a document library in my SharePoint Online Site. Though this is equally applicable to On Premise implementations of SharePoint.

I have added a new document library in the SharePoint Online Site with the name “Prashant Documents” and uploaded a set of documents to it.

0.0.png

I have also set up a local folder on the disk drive which will act as a destination folder for the files to be downloaded.

0.1.png

Step 1- Initiate the Client Context

In order to get more information on Setting up Client Context and other Environmental Configurations, you may visit one of the earlier articles SharePoint Online: How to Install SharePoint Online Management Shell.

1.png

Step 2

Instantiate and 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 within the “Microsoft.SharePoint.Client.File” class, we can read the file from the SharePoint Document Library and save the File Stream to the disk location by using the “CopyTo” method which is available in the “System.IO.File” Class.

2

Step 5

Call the “DownloadFilesFromFolder” function that will download the files to the disk location.

3

Step 6

On execution of this function, the Windows PowerShell Credentials prompt will appear where we have to specify the password to the login account.

4.png

Step 7

Once the script is executed successfully, we can see the documents downloaded to the disk location.

5.png

Hope you find this helpful.