Tuesday, May 8, 2012

Powershell - Getting Folders Size

How many times did you just wish to not only list some files but also have the file's folder's size be displayed?
Today i setup a little script to give a hand at this.

The output is from the Get-ChildItem cmdlet (dir) but extending the System.IO.DirectoryInfo
Objects output with a ScriptProperty that display the folder size.

It will do the job for now, but it just can use the default behavior of  Get-ChildItem.

;)

Function Get-ExtendedChildItem {

[cmdletbinding()]

Param(
[Parameter(Mandatory=$false)]
[string]$FolderPath
)

if($FolderPath)
{$d = Get-ChildItem $FolderPath}
else
{$d = Get-ChildItem}

$d | Where-Object {$_.PSIsContainer} |
ForEach-Object{
Add-Member -InputObject $_ -MemberType ScriptProperty -Name Length -Value `
{#Get
$size = 0
Get-ChildItem -Recurse $this.FullName | Where-Object {!$_.PSIsContainer} |
ForEach-Object {$size += $_.Length}
$size
}
}
$d
}

No comments:

Post a Comment