I was wanting to see how big my users’ home folders are. With out quotas in place, I decided to use powershell. I put together a function that will list the size of a folder that I pass to it. I wrote it in a way that if I pass a collection of folders to it, it will list the sizes of those folders individually. This way I can spot check individual folders or check every folder in the home folders location.
This works even better for me because I need it to list the sub folder of each folder in the home folder location. My home folders are placed in department folders. So I have to add that extra step to every script I use to make sure it processes at the right level. Allowing my function to work on a collection of folders allows powershell to handle this for me. Here is the syntax in action:
#Single folder
PS> Get-Item . | Get-FolderSize
#Sub folders
PS> Get-ChildItem . | Get-FolderSize
#Sub Sub Folders
PS> Get-ChildItem . | %{Get-ChildItem $_.FullName} | Get-FolderSize
It is required that you pass your directory objects to it. I may update it to allow you to pass a folder path later. But for my needs, this is exactly how I will use it in my scripts. Here is the full code for Get-FolderSize:
function Get-FolderSize ($_ = (get-item .)) {
Process {
$ErrorActionPreference = "SilentlyContinue"
$length = (Get-ChildItem $_.fullname -recurse | Measure-Object -property length -sum).sum
$obj = New-Object PSObject
$obj | Add-Member NoteProperty Name ($_.Name)
$obj | Add-Member NoteProperty FullName ($_.FullName)
$obj | Add-Member NoteProperty Length ($length)
$obj | Add-Member NoteProperty MB ("{0,13:N2}" -f ($length / 1MB))
Write-Output $obj
}
}
