get size of files in specific SharePoint directory with SharePoint CSOM

mm 21 Reputation points
2021-02-24T12:41:52.17+00:00

I have a script to synchronize local directory with SharePoint (upload and replace). Now I'd like to minimize unnecessary uploads, if file has not changed - current version is already at SP. However I am struggling with getting the file size and upload date from SP. First in my script, I am loading SharePoint libraries: SharePoint Online Client Components version 16. I tried to use File.Length constructor from File class, but I am getting an error:

$File = New-Object -TypeName Microsoft.SharePoint.Client.File
A constructor was not found. Cannot find an appropriate constructor for type Microsoft.SharePoint.Client.File

same for Folder:
$Folder = New-Object -TypeName Microsoft.SharePoint.Client.Folder
A constructor was not found. Cannot find an appropriate constructor for type Microsoft.SharePoint.Client.Folder

Even though I found it documented here: https://learn.microsoft.com/en-us/dotnet/api/microsoft.sharepoint.client.folder?view=sharepoint-csom
and .Client.Folder constructor was offered to me from intellisense too ..
What am I missing ? How can I get file size (File.Length) and upload date for all files in specific SharePoint directory ?

Microsoft 365 and Office SharePoint For business Windows
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Li Zhang_MSFT 1,566 Reputation points
    2021-02-25T08:27:12.643+00:00

    Hi @mm ,

    Please try following PowerShell :

    #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"   
      
    #Config Parameters   
    $SiteURL= "https://tenant.sharepoint.com/sites/sitename"   
    $ListName = "libraryname"   
    $CSVPath = "C:\Temp\DocumentsInventory.csv"   
      
    #Get Credentials to connect   
    $Cred = Get-Credential   
      
    Try {   
        #Setup the context   
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)   
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)   
      
        #Get the Document Library   
        $List =$Ctx.Web.Lists.GetByTitle($ListName)   
      
        #Define CAML Query to Get All Files   
        $Query = New-Object Microsoft.SharePoint.Client.CamlQuery   
        $Query.ViewXml = "@<View Scope='RecursiveAll'>    
                                <Query>   
                                    <Where>   
                                        <Eq>   
                                            <FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value>   
                                        </Eq>   
                                    </Where>   
                                </Query>   
                            </View>"   
      
        #powershell sharepoint online list all documents   
        $ListItems = $List.GetItems($Query)   
        $Ctx.Load($ListItems)   
        $Ctx.ExecuteQuery()   
      
        $DataCollection = @()   
        #Iterate through each document in the library   
        ForEach($ListItem in $ListItems)   
        {   
            #Collect data          
            $Data = New-Object PSObject -Property ([Ordered] @{   
                FileName  = $ListItem.FieldValues["FileLeafRef"]   
                RelativeURL = $ListItem.FieldValues["FileRef"]   
                CreatedBy = $ListItem.FieldValues["Created_x0020_By"]   
                CreatedOn = $ListItem.FieldValues["Created"]   
                ModifiedBy = $ListItem.FieldValues["Modified_x0020_By"]   
                ModifiedOn = $ListItem.FieldValues["Modified"]   
                FileSize = $ListItem.FieldValues["File_x0020_Size"]   
            })   
            $DataCollection += $Data   
        }   
        $DataCollection   
      
        #Export Documents data to CSV   
        $DataCollection | Export-Csv -Path $CSVPath -Force -NoTypeInformation   
        Write-host -f Green "Documents Data Exported to CSV!"   
    }   
    Catch {   
        write-host -f Red "Error:" $_.Exception.Message   
    }   
    

    After we run the PowerShell above, we will find csv file in C:\Temp\ , created/modified date and file size information are in it.

    71940-file-info.png

    Note: We can only get the date when the file was uploaded for the first time (Created) and the last modified date, that is, the time when the user modified or uploaded the file in SharePoint Online (Modified).

    Reference:

    https://www.sharepointdiary.com/2018/08/sharepoint-online-powershell-to-get-all-files-in-document-library.html

    Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link.

    ---------------------------------------------------------------------------------------------------------------------------------------------------------------

    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.