SharePoint Online: PowerShell to Get a File

Requirement: Get a File from SharePoint Online using PowerShell

PowerShell to Get a File from SharePoint Online
Here is my nifty collection of PowerShell scripts to get a file from SharePoint Online.
#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"
   
#Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/marketing"
$FileRelativeURL ="/sites/marketing/Shared Documents/Compliance Process.xlsx"

#Get Credentials to connect
$Cred = Get-Credential
 
Try { 
    #Set up the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl) 
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
 
    #powershell get file from sharepoint online
    $File = $Ctx.web.GetFileByServerRelativeUrl($FileRelativeURL)
    $Ctx.Load($File)
    $Ctx.ExecuteQuery()
 
    Write-host "File Size:" ($File.Length/1KB)
}
catch {
    write-host "Error: $($_.Exception.Message)" -Foregroundcolor Red
}

Get File from Absolute URL in SharePoint Online
#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"
   
#Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/marketing"
$FileURL = "https://crescent.sharepoint.com/sites/marketing/Shared Documents/Compliance Process.xlsx"

#Get Credentials to connect
$Cred = Get-Credential
 
Try { 
    #Set up the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl) 
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
 
    #powershell sharepoint online get file by url
    $File = $Ctx.web.GetFileByUrl($FileURL)
    $Ctx.Load($File)
    $Ctx.ExecuteQuery()
    
    Write-host "File Size:" ($File.Length/1KB)    
}
catch {
    write-host "Error: $($_.Exception.Message)" -Foregroundcolor Red
} 

SharePoint Online: PowerShell to Get File by Name from a Document 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"
   
#Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/marketing"
$ListName ="Documents"
$FileName = "Compliance Process.xlsx"

#Get Credentials to connect
$Cred = Get-Credential
 
Try { 
    #Set up the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl) 
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
 
    #Get List Items
    $List = $Ctx.web.Lists.GetByTitle($ListName)
    $ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()) 
    $Ctx.Load($ListItems)
    $Ctx.ExecuteQuery()

    #Get File from by Name
    $Item =  $ListItems | where {$_["FileLeafRef"] -eq $FileName} | Select -First 1

    If($Item -ne $Null)
    {
        #Get the File from List Item
        $File = $Item.File
        $Ctx.Load($File)
        $Ctx.ExecuteQuery()

        Write-host "File Size:" ($File.Length/1KB)
    }
    else
    {
        Write-host -f Yellow "File Not Found!"
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -Foregroundcolor Red
}

PowerShell to Get File from List Item in SharePoint Online with CAML Query
#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"
   
#Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/marketing"
$ListName ="Documents"
$FileName = "Compliance Process.xlsx"

#Get Credentials to connect
$Cred = Get-Credential
 
Try { 
    #Set up the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl) 
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
 
    #Get List
    $List = $Ctx.web.Lists.GetByTitle($ListName)

    #Frame CamlQuery to retrieve items by File name
    $CAMLQuery= New-Object Microsoft.SharePoint.Client.CamlQuery
    $CAMLQuery.ViewXml = "<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='Text'>$FileName</Value></Eq></Where></Query></View>"

    #Get List Items matching given File Name
    $ListItems = $List.GetItems($CAMLQuery)
    $Ctx.Load($ListItems)
    $Ctx.ExecuteQuery()

    #Get the First File matching File Name
    If($ListItems -ne $Null)
    {
        #sharepoint online powershell get file
        $File = $ListItems[0].File
        $Ctx.Load($File)
        $Ctx.ExecuteQuery()

        Write-host "File Size:" ($File.Length/1KB)
    }
    else
    {
        Write-host -f Yellow "File Not Found!"
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -Foregroundcolor Red
}
If you are looking for downloading a file, refer: PowerShell script to download file from SharePoint Online

Related posts:
SharePoint Online: PowerShell to Get a File SharePoint Online: PowerShell to Get a File Reviewed by Salaudeen Rajack on August 13, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.