SharePoint Online: PowerShell to Get a File
Requirement: Get a File from SharePoint Online using PowerShell.
PowerShell to Get a File from SharePoint Online
In this blog post, we will take a look at how you can use PowerShell to get a file from a SharePoint Online site. This can be useful if you need to access a file property, such as the file’s size or last modified date, etc. Let’s get started!
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
Here is the PowerShell to get a file by 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
Here is the PowerShell script to retrieve SharePoint Online document:
#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
}
SharePoint Online: Get a File using PowerShell
Here is how to access a file in SharePoint Online through PowerShell:
#Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/marketing"
$FileRelativeURL = "/sites/marketing/Shared Documents/Proposal Template.docx"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Get the File as List Item
$File = Get-PnPFile -Url $FileRelativeURL -AsListItem
#Get File Size
Write-host "File Size (KB):"$File["File_x0020_Size"]
Get Contents of a File Stored in SharePoint Online
How about reading the contents of a log file stored in a SharePoint Online document library?
#Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/PMO"
$FileRelativeURL = "/sites/PMO/Shared Documents/Log.txt"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Get the Contents of the File
$File = Get-PnPFile -Url $FileRelativeURL -AsString
Write-host $File
If you are looking to download a file, refer to PowerShell script to download a file from SharePoint Online
Related posts: