How to Download a File from SharePoint Library using PowerShell?

Requirement: Download a File from SharePoint Document Library using PowerShell.

How to Download a File from a Library in SharePoint?

Users can download copies of files from the SharePoint libraries to another location, such as the local computer or an accessible network file share. To download a copy of a file from a document library, do the following:

  1. Go to your SharePoint document library and select the file to download. 
  2. From the “Files” tab, click on the “Download a Copy” button from the ribbon. 
    download sharepoint file using powershell
  3. This opens the “Save as” pop-up. Click “Save as” and select a location where the document copy will be saved. Specify the name for the document and click on “Save”.

You’ll see the message “download has completed” at the bottom of the browser window. You can also use “Download a Copy” from the document’s context menu. Just click the ellipsis icon on the pop-up window and choose “Download a Copy”.

Download a File from SharePoint using PowerShell

If you are looking for a way to automate the process of downloading files from SharePoint, PowerShell is the perfect solution. This guide will show you how to use PowerShell to download SharePoint files quickly and easily.

Here is how to download a SharePoint File using PowerShell:

Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue

#Set Parameters
$WebURL = "https://opera.crescent.com/sites/template/"
$FileRelativedURL = "/Sites/template/Shared Documents/ASI-LOG.docx"
$LocalFolderPath = "C:\Template"

Try {
    #Ensure the destination local folder exists! 
    if (!(Test-Path -path $LocalFolderPath))
    {    
         #If it doesn't exists, Create
         $LocalFolder = New-Item $LocalFolderPath -type directory 
    }

    #Get the Fie
    $Web = Get-SPWeb $WebURL
    $File = $Web.GetFile($FileRelativedURL)

    #Download the file
    $Data = $File.OpenBinary()
    $FilePath = Join-Path $LocalFolderPath $File.Name
    [System.IO.File]::WriteAllBytes($FilePath, $Data)

    Write-host -f Green "File downloaded Successfully!"
}
Catch {
Write-host -f Red "Error:" $_.Exception.Message
}

The process is relatively straightforward and involves setting variables for the site URL, file server relative URL, and local folder location to download the file, and then using the GetFile method to retrieve the file and save it to your local system using OpenBinary and WriteAllBytes methods. This PowerShell method provides a convenient way to programmatically download files from a SharePoint Library.

To download all files from a SharePoint document library using PowerShell, use: Download All Files from SharePoint Document Library using PowerShell

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

Leave a Reply

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