1

I found a code snippet that loops through the files and folders in SharePoint, using PnP PowerShell, and retrieves the count. When I modify the code to also retrieve the last modified date and size for each file and folder, it doesn't work. Below is the code snippet:

#Connect to SharePoint Online
Connect-PnPOnline -Url $SiteUrl -Interactive

#Function to get number of Sub-folder and Files count recursively
Function Get-SPOFolderStats
{
  [cmdletbinding()]
 
    param
    ([Parameter(Mandatory=$true,ValueFromPipeline=$true)][Microsoft.SharePoint.Client.Folder]$Folder)
    #Get Sub-folders of the folder
    Get-PnPProperty -ClientObject $Folder -Property ServerRelativeUrl, Folders | Out-Null
 
    #Get the SiteRelativeUrl
    $Web = Get-PnPWeb -Includes ServerRelativeUrl
    $SiteRelativeUrl = $Folder.ServerRelativeUrl -replace "$($web.ServerRelativeUrl)", [string]::Empty
    $PNPFile   = Get-PnPFolderItem -FolderSiteRelativeUrl $SiteRelativeUrl -ItemType File
    $PNPFolder = Get-PnPFolderItem -FolderSiteRelativeUrl $SiteRelativeUrl -ItemType Folder

    **[PSCustomObject] @{
        Folder                   = $Folder.Name
        Path                     = $Folder.ServerRelativeUrl
        "Item Count"             = $PNPFile | Measure-Object | Select-Object -ExpandProperty Count
        "Sub-Folder Count"       = $PNPFolder | Measure-Object | Select-Object -ExpandProperty Count**
    }
     
    #Process Sub-folders
    ForEach($SubFolder in $Folder.Folders) {Get-SPOFolderStats -Folder $SubFolder}
}

1 Answer 1

0

First, I need to check with you, is it the addition of the modified code that is causing it not to work? Or is this code snippet not working in your program itself?

I refined the code you provided, successfully get files and folders in SharePoint and retrieves the count. The code is as follows:

#Parameters
$SiteURL = "https://xxxxx.sharepoint.com/sites/xxxxx"
$ListName = "test1"
 
#Function to get number of Sub-folder and Files count recursively
Function Get-SPOFolderStats
{
  [cmdletbinding()]
 
    param
    (
        [Parameter(Mandatory=$true,ValueFromPipeline=$true)][Microsoft.SharePoint.Client.Folder]$Folder
    )
    #Get Sub-folders of the folder
    Get-PnPProperty -ClientObject $Folder -Property ServerRelativeUrl, Folders | Out-Null
 
    #Get the SiteRelativeUrl
    $Web = Get-PnPWeb -Includes ServerRelativeUrl
    $SiteRelativeUrl = $Folder.ServerRelativeUrl -replace "$($web.ServerRelativeUrl)", [string]::Empty
 
    [PSCustomObject] @{
        Folder    = $Folder.Name
        Path      = $Folder.ServerRelativeUrl
        ItemCount = Get-PnPFolderItem -FolderSiteRelativeUrl $SiteRelativeUrl -ItemType File | Measure-Object | Select -ExpandProperty Count
        SubFolderCount = Get-PnPFolderItem -FolderSiteRelativeUrl $SiteRelativeUrl -ItemType Folder | Measure-Object | Select -ExpandProperty Count
    }
     
    #Process Sub-folders
    ForEach($SubFolder in $Folder.Folders)
    {
        Get-SPOFolderStats -Folder $SubFolder
    }
}
 
#Connect to SharePoint Online
Connect-PnPOnline $SiteURL -Credentials (Get-Credential)
 
#Call the Function to Get the Library Statistics - Number of Files and Folders at each level
$FolderStats = Get-PnPList -Identity $ListName -Includes RootFolder | Select -ExpandProperty RootFolder | Get-SPOFolderStats | Sort Path
$FolderStats

My test result:

enter image description here

Here is some documents on getting "the latest modified time" and "file size"

For reference: Get file last modified time

Get file Size

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.

1
  • I tried the below from the link that you posted for the last modified time and received an error:$(Get-PnpFolderItem -FolderSiteRelativeUrl $SiteRelativeUrl -ItemType File).ListItemAllFields["Modified"]
    – igarry
    Commented Apr 27, 2022 at 14:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.