Skip to content

Instantly share code, notes, and snippets.

@jamiechalmerzlp
Last active August 10, 2022 14:15
Show Gist options
  • Save jamiechalmerzlp/1da68c558861e505e41fe721db7fa514 to your computer and use it in GitHub Desktop.
Save jamiechalmerzlp/1da68c558861e505e41fe721db7fa514 to your computer and use it in GitHub Desktop.
Provides the ability for users to export a count for all Folders, Sub Folders and Files from specified directories using PnP Online
#Parameters - Remove /site/site if this is not applicable to you
$SiteURL = "https://XXXX.sharepoint.com/site/site"
Connect-PnPOnline $SiteURL -Interactive
$FolderSiteRelativeURL = "/Shared Documents/subfolder/subfolder"
$downloadPath = "C:\temp"
Function Get-SPOFolderStats
{
[cmdletbinding()]
param
(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)][Microsoft.SharePoint.Client.Folder]$Folder
)
Get-PnPProperty -ClientObject $Folder -Property ServerRelativeUrl, Folders | Out-Null
$Web = Get-PnPWeb -Includes ServerRelativeUrl
#$SiteRelativeUrl = $Folder.ServerRelativeUrl -replace "$($web.ServerRelativeUrl)", [string]::Empty
$SiteRelativeUrl = $Folder.ServerRelativeUrl.Substring($Folder.Context.Web.ServerRelativeUrl.Length)
[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
}
}
$folder = Get-PnpFolder $FolderSiteRelativeURL
$folderStats = Get-SPOFolderStats -Folder $folder
$outputFile = Join-Path $downloadPath "$($folder.name)_stats.csv"
$folderStats | Export-csv $outputFile -NoTypeInformation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment