Posted by: Preetam | December 19, 2006

Power Shell Folder Size

Yesterday I was going through the Virtual Labs for powershell, and found few more powers of shell. I will posting that in different post soon, I just need to format it. Live writer from MS makes life easy in formatting blogs. Here we go. I wished to find out folder size, similiar like treesize (But not tree size)

$TotalFoldSZ=0 #total folder size
$foltree=get-childitem “C:\Program Files\iTunes” #Which fold size you wish to check out, this can be easily turn into user’s by using read-host

foreach ($folt in $foltree) {

if($folt.mode -match “d”) { #I used mode attribute to get only directories, so what about files in the parent directory…hmm coming to in…:-)
$fsz=((get-childItem $folt.fullname -recurse Measure-object length -sum).sum)/1MB #Got the size in MB
FSize=”{0:N2}” -f $fsz #Formatted to two decimals
write-host $ $fsize MB # wrote to screen FolderName,FolderSize
$TotalFSZ=$TotalFSZ+$fsize #To get the total folder size
$filesize=(($foltree measure-object length -sum).sum)/1MB #This is where I check if there are files present in the parent folder. If there are #only files then I will get folder size zero, not to miss that out, I’ve to include it.
$fsizeMB=”{0:N2}” -f $filesize #Formatted it get in MB
Write-host Total Size of Folder is ($TotalFSZ+$fsizeMB) MB #here finally I add each folder size and files inside parent folder.

Most important thing in this script is the usage of ((get-childItem $folt.fullname -recurse Measure-object length -sum).sum)/1MB . It is just makes life easy, you get powershell to add the file size and report it.

Try this PS E:\> Get-ChildItem E:\PowerShell measure-object length -Average -Sum -Maximum -Minimum and you will be pleased to see ..yeah it gives number of files in the folder,Average size of the file and maxmimum file size and Minimum file size. Just a thing to note, you can manipulate only file items, I’ve to checkout if there is something which can do for folders as well.Next thing I would like someone to helpout with the sorting stuff.

Technorati: ,


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s


%d bloggers like this: