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.
Get File from Absolute URL in SharePoint Online
SharePoint Online: PowerShell to Get File by Name from a Document Library
PowerShell to Get File from List Item in SharePoint Online with CAML Query
SharePoint Online: Get a File using PowerShell
Related posts:
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 }
SharePoint Online: Get a File using 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 -UseWebLogin #Get the File as List Item $File = Get-PnPFile -Url $FileRelativeURL -AsListItem #Get File Size Write-host "File Size (KB):"$File["File_x0020_Size"]If you are looking for downloading a file, refer: PowerShell script to download file from SharePoint Online
Related posts:
No comments:
Please Login and comment to get your questions answered!