SharePoint Online: Download a File from Library using PowerShell

Requirement: Download a File from SharePoint Online using PowerShell

How to Download a File from SharePoint Online Library?

Downloading files from SharePoint Online is pretty straightforward. If you need to download a file from SharePoint Online, just browse to the site that contains the document, right-click on the document and select ‘Download’ in the menu bar – it’s that simple. This can be useful if you need to access a document that’s been shared with you – without any connectivity from your local machine. Let’s see the step-by-step instructions on how to download a file.

To download a file from the library, follow these steps:

  • Sign-in to SharePoint Online site >> Navigate to the library where your desired file is located
  • Right-click on the file and select the “Download” option from the context menu.
    sharepoint online powershell download file
  • This gets you the “Save” prompt and the file gets saved in your client machine’s Downloads directory.
  • In Modern Libraries, You can simply select the file(s) or folder and hit the “Download” button from the toolbar. You can also download multiple files at once.
    powershell download file from sharepoint online
You can open any document library or folder in “File Explorer” view and just copy-paste the files to your local machine to download them!

Now, How to download a file from SharePoint Online using PowerShell? Well, Here is the PowerShell script to download a file from SharePoint Online!

SharePoint Online PowerShell to Download File from Library

Let’s download documents from the SharePoint Online document library using PowerShell. This CSOM PowerShell script downloads a file from the SharePoint Online document library (or any library!)

#Load SharePoint CSOM Assemblies
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-FileFromLibrary()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $SourceFile,
        [Parameter(Mandatory=$true)] [string] $TargetFile
    )

    Try {
        #Setup Credentials to connect
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
    
        #sharepoint online powershell download file from library
        $FileInfo = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($Ctx,$SourceFile)
        $WriteStream = [System.IO.File]::Open($TargetFile,[System.IO.FileMode]::Create)
        $FileInfo.Stream.CopyTo($WriteStream)
        $WriteStream.Close()

        Write-host -f Green "File '$SourceFile' Downloaded to '$TargetFile' Successfully!" $_.Exception.Message
  }
    Catch {
        write-host -f Red "Error Downloading File!" $_.Exception.Message
    }
}

#Set parameter values
$SiteURL="https://crescent.sharepoint.com/sites/sales/"
$SourceFile="/sites/Sales/Shared Documents/Crescent Legal App Requirements.docx"  #Server Relative URL 
$TargetFile="C:\Temp\LegalDoc.docx"

#Call the function to download file 
Download-FileFromLibrary -SiteURL $SiteURL -SourceFile $SourceFile -TargetFile $TargetFile 

To download all files from a SharePoint Online document library, use this script: SharePoint Online download multiple files using PowerShell

Download a File from SharePoint Online using PnP PowerShell

We can copy files from SharePoint Online to a local disk with PowerShell. Here is the PnP PowerShell to download a file from SharePoint Online:

#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com/sites/marketing"
$FileRelativeURL = "/sites/Marketing/Shared Documents/Discloser Asia.docx"
$DownloadPath ="C:\Temp"

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred
    
    #powershell download file from sharepoint online
    Get-PnPFile -Url $FileRelativeURL -Path $DownloadPath -AsFile
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

This PowerShell copies file locally from SharePoint Online without prompt. To download a folder from SharePoint Online using PowerShell, use: PowerShell to Download Folder from SharePoint Online

If you want to download all files from all libraries in a SharePoint site, use: How to Download All Files from a SharePoint Site?

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!

12 thoughts on “SharePoint Online: Download a File from Library using PowerShell

  • How to download all Folders & Files from Document library with version history using powershell

    Reply
  • Doesn’t work.

    Get-PnPFile : Cannot convert ‘System.Object[]’ to the type ‘System.String’ required by parameter ‘Url’. Specified method is not supported.
    At line:15 char:18
    Get-PnPFile -Url $CSVData.FileRelativeURL -path $DownloadPath -AsFile
    ~~~~~~~~~~~~~~~~~~~~~~~~
    CategoryInfo : InvalidArgument: (:) [Get-PnPFile], ParameterBindingException
    FullyQualifiedErrorId : CannotConvertArgument,PnP.PowerShell.Commands.Files.GetFile

    Reply
  • Hi,
    is there a way to pass the credentials of the currently logged in user to the script?
    E.g. every user who gets the script via intune (so even outside of the Domain-Network and out of reach for the gpo’s) automatically downloads a file from the SharePoint to a specific location on the device.

    Many thanks in advance.

    Reply
  • Hello,

    I have a need to download a list of specific files from SharePoint Online that are from different libraries to a network location. Do you have anything that accomplishes that task. Currently i have the list of file paths in sharepoint in a .csv file. I would like to read in that .csv and download the files.

    Reply
    • Say, you have a CSV file with “FileRelativeURL” column. Here is the PowerShell script you can utilize:

      #Get Data from CSV File
      $CSVData = Import-CSV -path "C:\SitesData.csv"
      
      #Iterate through each Row in the CSV
      foreach ($Row in $CSVData)
      {
      #Download the File
      Get-PnPFile -Url $CSVData.FileRelativeURL -Path "C:\Downloads" -AsFile
      }
      
      Reply
  • This comment has been removed by the author.

    Reply
  • This is a great site for information. Thank you

    Do you have also sharepoint online powershell scripts that searches inside documents (docx, xlsx, pdf) for keywords? I could not find any.

    Reply
  • How to download a file from SharePoint on premise site based on current date? any help will be much appreciated

    Reply
  • How to download multiple files from sharepoint online using powershell. Above one is to download single file.

    Reply

Leave a Reply