About Sharepoint Powershell script

jennyKim 240 Reputation points
2024-03-06T07:10:59.4666667+00:00

I want to get the information of Sharepoint folders and files such as name,size and path of a document library in a site using powershell

I have create a script but the script didnot retrive the desire data so If anyone could help me in this would be appriciated

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,622 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,504 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,445 questions
{count} votes

Accepted answer
  1. Emily Du-MSFT 45,586 Reputation points Microsoft Vendor
    2024-03-07T05:59:17.7666667+00:00

    You could use following PowerShell to get information of folders and files such as name, path and size in a document library.

    #Parameters
    $SiteURL = "https://tenant.sharepoint.com/sites/emilytestnew"
    $ListName= "Documents"
    $ReportOutput = "C:\Info.csv"
       
    #Connect to SharePoint Online site
    Connect-PnPOnline $SiteURL -Interactive
     
    #Array to store results
    $Results = @()
       
    #Get all Items from the document library
    $List  = Get-PnPList -Identity $ListName
    $ListItems = Get-PnPListItem -List $ListName -PageSize 5000
     
    $ItemCounter = 0
    #Iterate through each item
    Foreach ($Item in $ListItems)
    {
            $Results += New-Object PSObject -Property ([ordered]@{
                FileName          = $Item.FieldValues.FileLeafRef
                RelativeURL       = $Item.FieldValues.FileRef
                FileSize          = $Item.FieldValues.File_x0020_Size
                TotalFileSize     = $Item.FieldValues.SMTotalSize.LookupId
            })
        $ItemCounter++
        Write-Progress -PercentComplete ($ItemCounter / ($List.ItemCount) * 100) -Activity "Processing Items $ItemCounter of $($List.ItemCount)" -Status "Getting data from Item '$($Item['FileLeafRef'])"
    }
    #Export the results to CSV
    $Results | Export-Csv -Path $ReportOutput -NoTypeInformation
    Write-host "Report Exported to CSV Successfully!"
    

    Result:User's image


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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.