I had this same question, with a few tweaks, but I had a problem most of the other answers here don't address - what if your document library has sub-folders?
I've written a script that will give you the number of actual files in a document library (that's not including folders) and the total size of everything in the library, recursively.
# Gets an object with NumFiles and TotalSize properties,
# which correspond with those of the list you pass in.
function Get-ListFileInfo($SPList)
{
$name = $SPList.Title
Write-Verbose " $($name)"
if($SPList.BaseType -eq "DocumentLibrary")
{
$fileInfo = New-Object –TypeName PSObject –Prop (
@{'NumFiles'=0;
'TotalSize'=0}
)
$folder = $SPList.RootFolder
$fileInfo = Get-DocLibFolderFileInfo $folder $true
$fileInfo.NumFiles = $SPList.Items.Count
}
# You could expand this to other list types, too.
# I left that out for brevity's sake.
Write-Output $fileInfo
}
# Recursive function to get the filesize information from a folder
# Pass this the RootFolder of your SPList and it'll return the filesizes.
# used to count Files in here, too, but modified for accuracy
function Get-DocLibFolderFileInfo($SPFolder, [boolean] $recursive = $true)
{
$filesInfo = New-Object –TypeName PSObject –Prop (
@{'NumFiles'=$null;
'TotalSize'=0})
Write-Verbose " Folder: $($SPFolder.Name)"
if($SPFolder.Name -ne "Forms")
{
$files = $SPFolder.Files
$filesHere = $files.Count
Write-Verbose " # Files: $($filesHere)"
for($etr = 0;$etr -lt $filesHere; $etr++)
{
#$item = $files[$etr]
$file = $files[$etr]
#$file = ($item.file)
if($file -ne $null)
{
if($file.Name.EndsWith(".aspx"))
{
# skip views and the like, because we don't care.
Write-Verbose " Skipping File: $($file.Name)";
}
else
{
$fileSize = $file.Length
$filesInfo.TotalSize += $fileSize
Write-Verbose " Filename: $($file.Name)"
Write-Verbose " Size: $($fileSize)"
}
}
else
{
Write-Warning " Null file found?"
}
}
}
# Now recurse into subfolders
if($SPFolder.SubFolders.Count -ne 0 -AND $recursive -eq $true)
{
$subFolders = $SPFolder.SubFolders
for($etr = 0;$etr -lt $subFolders.Count; $etr++)
{
$subFolder = $subFolders[$etr]
$subFolderInfo = Get-DocLibFolderFileInfo $subFolder $true
$filesInfo.TotalSize += $subFolderInfo.TotalSize;
}
}
Write-Output $filesInfo
}
So to use these functions, just pass in your SPList
object to Get-ListFileInfo
and deal with the information it returns.