SharePoint Online: Download a File from Library using PowerShell

Requirement: Download a File from SharePoint Online using PowerShell

How to Download File from SharePoint Online Library?
To download file from 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 "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 "Download" button from the toolbar.
    powershell download file from sharepoint online
Now, How to download a file from SharePoint online using PowerShell?

SharePoint Online PowerShell to Download File from Library
This PowerShell script downloads file from 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"  #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 
Here is the PnP PowerShell to download 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
}
To download a folder from SharePoint Online using PowerShell, use: PowerShell to Download Folder from SharePoint Online
SharePoint Online: Download a File from Library using PowerShell SharePoint Online: Download a File from Library using PowerShell Reviewed by Salaudeen Rajack on September 22, 2016 Rating: 5

5 comments:

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

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

    ReplyDelete
  3. 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.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.