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

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

Answer accepted by question author
  1. Emily Du-MSFT 51,941 Reputation points Microsoft External Staff
    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' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.