0

Is there a way with SharePoint online in 2021 and SharePoint 2013, to easily find out how many documents are stored within a single document library? Same kind of question, two different approaches I am assuming.

2 Answers 2

2

Server:

$web = Get-SPWeb https://webUrl
$list = $web.Lists['Doc Lib Display Name']
$list.ItemCount

Online:

Connect-PnPOnline https://tenantName.sharepoint.com/sites/siteName
$list = Get-PnPList 'Doc Lib Display Name'
$list.ItemCount
2
  • Trever, for the online process, is having PowerShell good enough as a prerequisite for processing this code? or do I need more to the setup process?
    – klewis
    Commented Jan 28, 2021 at 18:44
  • 1
    You need the PnP module for the Online process powershellgallery.com/packages/PnP.PowerShell. The on-prem is obviously executed via the SharePoint Management Shell.
    – user6024
    Commented Jan 28, 2021 at 18:55
2

SharePoint Online: Get Files, Sub-Folders Count at Each Folder in a Document Library using PowerShell

#Parameters
$SiteURL = "https://test.sharepoint.com/sites/projects"
$ListName = "Documents"
 
#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)", ""
 
    [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
 
#Export to CSV
#$FolderStats | Export-Csv -Path "C:\Temp\DocLibStats.csv" -NoTypeInformation

enter image description here

SharePoint 2013: Get Files, Folders Count in a Document Library using PowerShell

$fileCount = 0
$folderCount = 0
$itemcount = 0

$Web = Read-host “Please enter the SPWEB Url”
$WebObject = get-spweb -Identity $Web
#$WebObject.Lists | Select Title
$Library = Read-Host “Please enter the Library or list name”
$LibraryObject = $WebObject.Lists[“$Library”]

$itemcount = $LibraryObject.ItemCount

foreach ($folders in $LibraryObject.Folders)
{
$folder = $folders.Folder
$folderCount ++

    foreach ($file in $folder.Files)
    {
        $fileCount ++
        $filesizeinkb = ($file.length/1024)
        “{0}`t{1}`t{2}” -f $folder.Name, $file.Name, $filesizeinkb   | out-file C:\SpFileExport.csv -Append     
    }
   
}
Write-Host -ForegroundColor Green “Total Item Count ” $itemcount
Write-Host -ForegroundColor Green “Total File Count ” $fileCount
Write-Host -ForegroundColor Green “Total Folder Count ” $folderCount

$WebObject.Dispose()

enter image description here

1
  • Thanks so much for the examples! In regards to SP2013 did you have to install any software apps to get that logic processed? Also do I "have to" be on the physical server to execute such logic? or can I do that remotely? Many thanks.
    – klewis
    Commented Apr 22, 2021 at 13:48

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.