Showing posts with label Powershell SharePoint 2010 List document library files size. Show all posts
Showing posts with label Powershell SharePoint 2010 List document library files size. Show all posts

Thursday, December 15, 2011

List all files in document library along with file sizes

Created a Powershell script to list all the files and there sizes into a comma separated file.

<# .SYNOPSIS Checks file sizes in a SharePoint document library and write them into comma separated file. #>
Add-PSSnapin Microsoft.SharePoint.PowerShell

$url = "your sharepoint site path"

$spAssignment = Start-SPAssignment


$strPath="$env:UserProfile\Documents\FileSizes.txt"
Add-Content $strPath "`n FileName , Size , Access Denied"
$spWeb = Get-SPWeb $url -AssignmentCollection $spAssignment

# Get the folder
$spFolder = $spWeb.GetFolder("Shared Documents")

# Store file collection in a variable

function FileParser([Microsoft.SharePoint.SPFolder]$spFolder)
{
$spQuery = new-Object Microsoft.SharePoint.SPQuery
$spQuery.Folder = $spFolder

$spWeb = $spFolder.ParentWeb

$spList = $spWeb.Lists[$spFolder.ParentListId]

$spItemCollection = $spList.GetItems($spQuery)


foreach ($spItem in $spItemCollection)

{

# If the item is a folder
Try
{
if ($spItem.Folder -ne $null)

{

# Write the Subfolder information

Write-Host("Folder: " + $spItem.Name + " Parent Folder: " + $spFolder.Name)

# Call the Get-Items function recursively for the found sub-solder

FileParser $spItem.Folder

}

# If the item is not a folder
if ($spItem.Folder -eq $null)
{

Write-Host("File: " + $spItem.File + " Size " + $spItem.File.Length)
$size = $spItem.File.Length.ToString()
$filepathName = $spItem.File.ToString()
$strLine = $filepathName + " , " + $size
Add-Content $strPath $strLine
}
}
Catch {
Write-Host("Error : $_ ")
Add-Content $strPath "$_"
}

}

$spWeb.dispose()

$spWeb = $null

}

FileParser $spFolder


$a = $null
[GC]::Collect()

Stop-SPAssignment $spAssignment

<# Acknowledgement: Part of the logic was taken from Paul King's blog http://blogs.msdn.com/b/paulking/ #>