Download SharePoint Documents from CSV File using PowerShell

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

PowerShell Script to Download Documents from SharePoint :
My CSV has "URL" and "FileName" columns in it, and here is the PowerShell to download SharePoint documents from a CSV file to local disk or network drive.
Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue

#Set Parameters 
$SiteURL = "http://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 downlod 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 = "http://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 downlod 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++
}

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.