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 is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

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

  • 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