# ============================================================================================== # # Microsoft PowerShell Source File -- Created with SAPIEN Technologies PrimalScript 2009 # # NAME: Get-FolderUsage.ps1 # # AUTHOR: Jeffery Hicks, PowerShell MVP , SAPIEN Technologies, Inc. # DATE : 2/26/2009 # # COMMENT: A simple function to return folder usage showing the filepath, the # total number of files, the average file size, the largest file size and the total size. # File sizes are in MB. # # The function is written to take a directory name as a pipelined input. # # DISCLAIMER AND WARNING: # THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY # KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. # TEST THOROUGHLY IN A NON-PRODUCTION ENVIRONMENT. IF YOU DON'T KNOW WHAT THIS # SCRIPT WILL DO...DO NOT RUN IT! # ============================================================================================== Function Get-FolderUsage { BEGIN { #set to Continue to enable debug messages $debugPreference="SilentlyContinue" Write-Debug ("{0} Begin" -f (Get-Date)) } PROCESS { #this step is optional, but it makes it easier I think #to follow the code [string]$fldr=$_ Write-Debug ("{0} Enumerating {1}" -f (Get-Date),$fldr) dir $fldr -recurse -force -ea "SilentlyContinue" | Measure-Object length -sum -max -average | Select-Object @{name="Folder";Expression={$fldr}},` @{name="Files";Expression={$_.count}},` @{name="Largest";Expression={"{0:F2}" -f ($_.maximum/1MB)}},` @{name="Average";Expression={"{0:F2}" -f ($_.average/1MB)}},` @{name="TotalSize";Expression={"{0:F2}" -f ($_.sum/1MB)}} } #end Process script block END { Write-Debug ("{0} End" -f (Get-Date)) } } #Sample usage # $env:temp | Get-FolderUsage # "c:\test","$env:userprofile\*documents" | Get-FolderUsage | sort "TotalSize" -desc | Format-Table -AutoSize # dir (Split-Path $env:userprofile) | foreach { # $_.fullname | Get-FolderUsage # } | sort Files -descending | # Format-Table Folder,Files,Largest,TotalSize -autosize