Download Files from SharePoint Using PowerShell [Efficiently]
SharePoint is highly used in organizations to manage their regular workflow. This also acts as a document management tool that provides real-time collaboration. However, it is not necessary to work with the documents online every time. Sometimes there is a need to download files from SharePoint using PowerShell or the admin center.
Using the SharePoint admin center, it is easy to download multiple files from SharePoint. On the other hand, this task becomes complex using PowerShell commands. But by applying the PowerShell commands, you can automate this process.
In this guide, we will discuss both approaches in detail so that you can pick the best one according to your technical expertise.
Steps to Download Files from SharePoint Using Admin Center
Let’s go deeper into the steps of the SharePoint admin center to download multiple files.
- Open SharePoint and sign in with the required credentials.
- Move to the library where the file is located.
- Press right-click on the file which you want to download and hit the Download button.
- You can also select multiple files and then hit the Download button. This will download a zip file that you can extract easily.
Download Files from SharePoint Using PowerShell
It looks difficult to download files from SharePoint using the PowerShell commands. But after knowing it once, you can perform this task seamlessly. Let’s have a look at the prerequisites that you should follow before starting executing the PowerShell commands.
Prerequisites
- You should have PowerShell installed on your machine.
- You need to update SharePoint Online management shell if it is not yet.
- Make sure you have appropriate credentials.
- Ensure your machine is connected to an efficient bandwidth Internet connection.
PowerShell commands
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll" Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll" Function Download-SharePoint_File() { param ( [Parameter(Mandatory=$true)] [string] $SharePoint_Site_URL, [Parameter(Mandatory=$true)] [string] $Source_SP_File_URL, [Parameter(Mandatory=$true)] [string] $Target_File_Path ) Try { $Credt= Get-Credential $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Credt.Username, $Credt.Password) $Ctxs= New-Object Microsoft.SharePoint.Client.ClientContext($SharePoint_Site_URL) $Ctxs.Credentials = $Credentials $File_Info = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($Ctxs,$Source_SP_File_URL) $WriteStream = [System.IO.File]::Open($Target_File_Path,[System.IO.FileMode]::Create) $File_Info.Stream.CopyTo($WriteStream) $WriteStream.Close() Write-host -f "File '$Source_SP_File_URL' Downloaded to '$Target_File_Path' Successfully!" $_.Exception.Message } Catch { write-host -f "Error While Downloading the Requested File!" $_.Exception.Message } } $SharePoint_Site_URL="Enter Complete Site URL" $Source_SP_File_URL="Enter Here" $Target_File_Path="D:\Temp\accounts.docx" Download-SharePoint_File -SharePoint_Site_URL $SharePoint_Site_URL -Source_SP_File_URL $Source_SP_File_URL -Target_File_Path $Target_File_Path
PnP PowerShell Commands to Download Multiple Files from SharePoint Using PowerShell
Execute the below PnP PowerShell commands to save SharePoint files without any hindrances.
$SharePoint_Site_URL = "Enter SharePoint Site URL Here" $File_Server_Relative_URL = "Provide Here" $Destination_Loc ="C:\Temp" Try { If(!(Test-Path -Path $Destination_Loc)) { New-Item -ItemType Directory -Path $Destination_Loc | Out-Null Write-host -f "A new folder is created '$Destination_Loc'" } Connect-PnPOnline -Url $SharePoint_Site_URL -Interactive $File = Get-PnPFile -Url $File_Server_Relative_URL -ErrorAction SilentlyContinue If($File -ne $Null) { Get-PnPFile -Url $File_Server_Relative_URL -Path $Destination_Loc -Filename $File.Name -AsFile -Force } Else { Write-host "Failed to Find File at "$File_Server_Relative_URL -f } } Catch { write-host "Run time error” }
How to Download and Read a CSV File from SharePoint?
Go with the below commands to download the CSV file from SharePoint.
$Site_URL = "Enter Here" $File_Server_Relative_URL = "/sites/online_sales/report/jan.csv" Connect-PnPOnline -Url $Site_URL -Interactive $Download_Path = $env:TEMP $Name_of_File = Split-Path $File_Server_Relative_URL -leaf Get-PnPFile -Url $File_Server_Relative_URL -Path $Download_Path -Filename $Name_of_File -AsFile -Force $CSV_File = Import-Csv -Path $env:TEMP\$Name_of_File
Walk-Around
Now, you are aware of how to download files from SharePoint using PowerShell or the admin center. Using PowerShell commands to perform this task can be error-prone. So, to avoid the data loss or data corruption while downloading files from SharePoint. It is necessary to perform SharePoint Migration for storing data to another account for future reference.
Final Words
In this write-up, we have discussed how to download files from SharePoint using PowerShell and the admin center. You can choose any of the methods according to your SharePoint expertise. Also, remember to have a backup of SharePoint before going to download its files.