I have a commandlet in our module that I use to pull library file counts, I don't know exactly what you are trying to accomplish, but could be a useful base. You pass it in a URL and it will iterate over every list.
function Get-AllListItemCountsForWeb{
<#
.SYNOPSYS
Generates a report of all lists and their item counts for a web
.DESCRIPTION
Generates a report of all lists and their item counts, for a web, excludes APP sites
.PARAMETER WebUrl
The url of the site to generate the report on
.PARAMETER GridView
If this switch is used, it will generate a grid view instead of an export
.EXAMPLE
Generates a CSV export
Get-AllListItemCountsForWeb -WebUrl "https://tenant.sharepoint.com/teams/eric"
.EXAMPLE
Opens a filterable grid view
Get-AllListItemCountsForWeb -WebUrl "https://tenant.sharepoint.com/teams/eric" -GridView
#>
[CmdletBinding()]
[OutputType([int])]
Param
(
[Parameter(Mandatory=$true, HelpMessage="The URL to the site that contains the list or library", Position=0)]
[string]$WebUrl,
[Parameter(Mandatory=$false, Position=1)]
[switch]$GridView
)
Begin{
$context = New-Object Microsoft.SharePoint.Client.ClientContext($WebUrl)
$context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credential.UserName, $credential.Password)
$web = $context.Web
$lists = $web.Lists
$context.Load($web)
$context.Load($lists)
$context.ExecuteQuery()
}
Process{
$Result = @()
if ($web.WebTemplate -ne "APP"){
foreach($list in $lists){
$count = Get-ListItemCount -WebUrl $WebUrl -ListName $list.Title
$Result += New-Object PSObject -Property @{
'Library Title' = $list.Title
Count = $count
'Site Title' = $web.Title
URL = $WebUrl
'Library Type' = $list.BaseType
}
}
}
}
End{
if($GridView){
$Result | Select 'Site Title',URL,'Library Type','Library Title',Count | Out-GridView
}
else{
$filename = "C:\Users\$env:USERNAME\Desktop\"+$web.Title.Replace(" ", "_")+"_ListReport.csv"
$Result | Select 'Site Title',URL,'Library Type','Library Title',Count | Export-Csv $filename -NoTypeInformation
Write-Host "Report saved to $filename" -ForegroundColor Green
}
$context.Dispose()
}
}
This in turn calls another function
function Get-ListItemCount{
<#
.SYNOPSYS
Returns the Item Count for the specified list or library
.DESCRIPTION
Returns the Item Count for the specified list or library
.PARAMETER WebUrl
The URL to the site that contains the list or library
.PARAMETER ListName
The display name of the list or library to get a count for
.EXAMPLE
Get-ListItemCount -WebUrl "https://tenant.sharepoint.com/teams/eric" -ListName "Shared Documents"
#>
[CmdletBinding()]
[OutputType([int])]
Param
(
[Parameter(Mandatory=$true, HelpMessage="The URL to the site that contains the list or library", Position=0)]
[string]$WebUrl,
[Parameter(Mandatory=$true, HelpMessage="The display name of the list or library to get a count for", Position=1)]
[string]$ListName
)
Begin{
$context = New-Object Microsoft.SharePoint.Client.ClientContext($WebUrl)
$context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credential.UserName, $credential.Password)
}
Process{
try{
$web = $context.Web
$list = $web.Lists.GetByTitle($ListName)
$context.Load($list)
$context.ExecuteQuery()
return $list.ItemCount
}
catch{
Write-Host -ForegroundColor Red $_.Exception.Message
return 0
}
}
End{
$context.Dispose()
}
}