SharePoint Online: Download Files from a CSV using PowerShell
Requirement: Download all files from a CSV file in SharePoint Online
PowerShell Script to Bulk Download All Files from a CSV in SharePoint Online
In this blog post, we will walk you through the process of downloading files from a SharePoint Online site. We’ll be using a CSV file as our source for the download and use PowerShell to download files from SharePoint Online.
I have got a number of files in Excel file and wanted to download them all to my local machine. Here is my CSV file – Tab-separated text file (Because file names may have commas in it, and CSV will break!)
This PowerShell script downloads all files from a given SharePoint Online site. Creates a log file of all possible failed ones, E.g., The file doesn’t exist on the site!
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/KPMG"
$CSVFile = "C:\Temp\KPMG.txt" #Tab Separated file
$DownloadPath = "C:\Temp\KPMG"
$LogFile = "C:\Temp\Error.log"
Try {
#Get Data from CSV File
$CSVData = Import-CSV -path $CSVFile -Encoding UTF8 -Delimiter "`t"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteUrl -UseWebLogin
#Iterate through each Row in the CSV
foreach ($Row in $CSVData)
{
#Prepare the Local Folder
$FileDownloadPath = $DownloadPath + $Row.ServerRelativeURL.Replace("/","\")
$FileName = Split-Path $FileDownloadPath -leaf
$ParentFolder = Split-Path $FileDownloadPath -Parent
#Create the Target Folder if it doesn't exists
If(!(test-path $ParentFolder))
{
New-Item -ItemType Directory -Force -Path $ParentFolder | Out-Null
}
Try {
Get-PnPFile -Url $Row.ServerRelativeURL -Path $ParentFolder -AsFile -ErrorAction Stop
Write-host "Downloaded File" $Row.ServerRelativeURL
}
Catch {
write-host -f Red "Error Downloading File $($Row.ServerRelativeURL) :" $_.Exception.Message
$Row.ServerRelativeURL | Out-File $LogFile -Append
}
}
}
Catch {
write-host -f Red "Error:" $_.Exception.Message
}
Hello!
you script works fine, except that it ask for the name of the file.
if I write any name, it will save the structure and the file with the name that I set.
actually the command get-pnpfile need to be filled with -filename, but for some reason I can’t import any CSV o column with the name related with the file.
How can I solve this?