SharePoint 2016/2013/Online: How download SharePoint List Item Attachments by PowerShell Automation

Downloading List Item Attachments is normally required when we are dealing with migration scenarios and sometimes for Custom Backups.

In this article we will explore the PowerShell to download the List Item attachments.

In order to setup this demo I have created a list and added 1 List Item with some of the attachments as shown below:


Also I have one folder in the local machine as shown below:


In the upcoming section we will do the Step by Step Code Analysis to get better understanding on the core functionality.

Step 1: In this step we will initialize the SharePoint Client Context by passing required credentials


Step 2: In this step we will initialize the SharePoint List Object by referencing it using current Client Context and setting up the disk location where the attachments will get downloaded

Step 3: In this step will first check the status of any pending request in the queue, and if there is any pending query then first execute it

Step 4: In this Step we will read the file from SharePoint List Item Attachments collection & save it to the disk location as specified in Step 2


Step 5: In this step finally will be call the “DownloadListItemAttachments” Function that will execute the logic we have seen in above steps


Once you execute the PowerShell it will ask you for the credentials as per the SharePoint Hosting Environment (On Premise or Online).

Provide the required credentials


If the PowerShell scripts executes successfully it will download all the attachment in the disk location as shown below:



During the real production scenarios it is always advisable to keep all attachments of a specific item in a separate folder govern by a proper naming convention to support smooth tracking of the content later on.

That is all for this demo.

Hope you find it helpful.

SharePoint 2016/2013/Online: How to Upload Files to SharePoint Document Libraries by PowerShell Automation

File upload operations are quite frequently used and often tricky when the SharePoint governance allows only SharePoint Client Side Technologies.

Recently I have come across a requirement in one of my assignments where an automated process is required to Sync the network shares with SharePoint.

This provides me the opportunity to write down the automation process using PowerShell +CSOM combination.

In this demo we will explore the PowerShell code to upload the file to SharePoint Online/On Premise Implementations which represents just a part of the complete automation process.

In order to setup this demo I have created as document library in SharePoint Online Site as shown below:


Let’s consider that we have a File Share somewhere on the network having files to be uploaded to the SharePoint Document Library


Now let’s explore the code step by step:

Step 1: In this step Client connection has been setup and the Client Context has been initiated.

The below is representing the connection to SharePoint Online Site but this will be little different in case if you want to initiate the connection with SharePoint On Premise site there will be a slight difference in the code.

For details you can visit to my earlier blogs at below URLs:


Step 2: In this step we will instantiate the Document Library Object that will provide us the handle on the desired document library in SharePoint Online Site.

This handle will let us allow getting reference of the desired folder in the document library. In this demo we are referring the Root Folder of the document library

Step 3: In this step we will explore the File System folder by using Get-ChildItem cmdlet on the using the specific Folder path

Step 4: In this step we will call another supporting function to upload the files to SharePoint Document Library


Inside UploadFile function-

Step 5: In this step File Object has been initialized using “System.IO” namespace. The file object will provide us the handle on files in the disk folder, open the file and read the content of the file in form of File Stream

Step 6: In this step we will add the file to the SharePoint Document Library folder (Root Folder in this case) as File Stream that we get it in Step 5

Step 7: Load File object & execute query to get it file actually uploaded to SharePoint Document Library

Step 8: Check in the file that has been uploaded in the Step 7

Step 9: This is showing the function call to “UploadFileToLibrary” function


That is all for the code.

In order to test this script we can either choose SharePoint (2016/2013/Online) Management Shell or Windows PowerShell.

Since in this demo I demonstrating the PowerShell in Conjunction with CSOM that I am going to run on client machines where we don’t have SharePoint Management Shell installed so I am going to use Windows PowerShell only.

Search for Windows PowerShell and launch the Windows PowerShell Command Prompt as shown below:


Refer the script & execute it


Provide password to when asked for


Once the PowerShell Script executes successfully, we can see the success messages printed on the Command Prompt


After the successful execution of the PowerShell script we can go back to the SharePoint Online Site and navigate to the document library that is the destination for these newly uploaded documents and sure enough we will get the documents


In case if you want to achieve additional automation scenarios you can execute this script as part of the Scheduled Task and that will further reduce human errors by executing the process automatically for you.

That is all for this demo.

Hope you find it helpful.