Download SharePoint Documents from CSV File using PowerShell

Requirement: Download files in the SharePoint document library from a CSV file to a local disk.

PowerShell Script to Download Documents from SharePoint :

If you need to quickly and easily download your SharePoint documents from a CSV file, PowerShell is the tool for you. This quick guide will show you how to import data from a CSV file and then download documents from the CSV file using PowerShell.

My CSV has “URL” and “FileName” columns in it, and here is the PowerShell to download SharePoint documents from a CSV file to a local disk or network drive.

Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue

#Set Parameters 
$SiteURL = "https://intranet.crescent.com" 
$CSVFilePath = "C:\Docs\DocList.csv"
$DownloadPath = "C:\Docs\Download\"
$Counter = 1

Try {
    #Get the data from CSV file
    $Web =  Get-SPWeb $SiteURL
    $CSVFile = Import-CSV $CSVFilePath
   
    #Read CSV file and download each file
    ForEach($Row in $CSVFile)
    {
        #Get the File
        $File = $Web.GetFile($Row.URL)

        #Download the File
        $Data = $File.OpenBinary()
        $FilePath= Join-Path $DownloadPath $Row.FileName
        [System.IO.File]::WriteAllBytes($FilePath, $Data)
        Write-host -f Green "`tDownloaded the File ($Counter of $($CSVFile.Count)):"$File.ServerRelativeURL
        $Counter++
    }
}
Catch {
    write-host -f Red "`tError:" $_.Exception.Message
}

The script gives progress in screen with messages as it downloads documents.

Download Files from a CSV using PowerShell

In another situation, I had List Item Ids stored in a CSV file and wanted to download files from CSV from a SharePoint Document Library.

Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue

#Parameters
$SiteURL = "https://crescent.sharepoint.com" 
$CSVFilePath = "C:\Temp\FilesList.csv"
$DownloadPath = "\\fileserver\WFDocs\"
$ListName ="WorkflowAttachments"

#Get the data from CSV file
$Web =  Get-SPWeb $SiteURL
$List = $Web.Lists[$ListName]
$CSVFile = Import-CSV $CSVFilePath
   
$Counter =1
#Read CSV file and download each file
ForEach($Row in $CSVFile)
{
    #Get the List Item by ID from CSV
    $ListItem = $List.GetItemByID($Row.ID)

    #Get the File
    $File = $ListItem.File
 
    #Download the File
    $Filename = $File.Name
    $Data = $File.OpenBinary()
    $FilePath= Join-Path $DownloadPath $Filename
    [System.IO.File]::WriteAllBytes($FilePath, $Data)
    Write-host -f Green "`tDownloaded the File: $FileName ($Counter of $($CSVFile.Count)) at "$File.ServerRelativeURL
    $Counter++
}

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. Passionate about sharing the deep technical knowledge and experience to help others, through the real-world articles!

4 thoughts on “Download SharePoint Documents from CSV File using PowerShell

  • Salaudeen,

    Trying to run against a SharePoint on Prem 2016 library

    The script is throwing the following.

    Error: Exception calling “OpenBinary” with “0” argumets(s): ” ”

    Any thoughts?

    Thank you,

    Reply
  • This doesn’t work for SharePoint Online. I’m trying to do the exact same thing using a csv file but I need to download the content from SharePoint Online instead. Do you have a script for that?

    Reply
  • Can we get to the sub-folder/s level with doc IDs and get the files downloaded.

    please advise?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *