Cash Talks - BS Walks RSS 2.0
# Wednesday, 16 January 2008

Today I went in search of a PowerShell script to calculate the size of a directory.  I was amazed to find very little.  Even worse, I was disappointed in what I found.  Here’s an example:

 

http://iformattable.blogspot.com/2007/07/powershell-script-to-determine.html

 

Cleaned up a little it looks like:

 

function Get-DirectorySize

{

      param ($path = {(Get-Location).Path})

     

      $size = Get-ChildItem $path -recurse -force `

            | ? { $_.GetType() -like 'System.IO.DirectoryInfo'} `

            | % {$_.GetFiles() } `

            | Measure-Object -Property Length -Sum `

            | Measure-Object -Property Sum -Sum 

     

      $size2 = (Get-item $path).GetFiles() `

            | Measure-Object -Property Length -Sum 

     

      [System.Math]::Round(($size.Sum + $size2.Sum) /  (1024 * 1024)).ToString() + "MB"

}

 

Returning a String is ridiculous.  If you are going to all the trouble of using Measure-Object you might as well get a rich output.

 

function Get-DirectorySize

{

      param ($path = {(Get-Location).Path})

     

      Get-ChildItem $path -recurse -force `

            | ? { $_.GetType() -like 'System.IO.FileInfo'} `

            | Measure-Object -Property Length -Sum -Average -Minimum -Maximum`

}

 

This is cleaner and has more interesting output providing an aggregated object with Sum, Average, Minimum and Maximum properties.  However, the biggest problem is that is extremely SLOW.  Try to use this on a directory with thousands of small files.  Even worse, try to use it on a network path with thousands of small files!

 

This approach tries to leverage all the cool PowerShell piping and CmdLets.  I prefer these to the old school VBScript and COM object approaches.  However, sometimes old school beats new school. 

 

function Get-DirectorySize

{

      param ($path = {(Get-Location).Path})

     

      if (! $GLOBAL:_FileSystemObject)

      {

            $GLOBAL:_FileSystemObject = New-Object -comobject "Scripting.FileSystemObject"

      }

      ($GLOBAL:_FileSystemObject.GetFolder($path)).Size

}

 

The old FileSystemObject is hard to beat for this kind of job.  In this example, I assure I don’t have to make more than one FileSystemObject. 

 

function Get-DirectorySize

{

      begin

      {

            if (! $GLOBAL:_FileSystemObject)

            {

                  $GLOBAL:_FileSystemObject = New-Object -comobject "Scripting.FileSystemObject"

            }

            $accumulation = 0

      }

      process

      {

            if ($_ -eq $null)

            {

                  $_ = (Get-Location).Path

            }

           

            if ($_ -is [System.IO.DirectoryInfo])

            {

                  $_ = $_.FullName

            }

           

            $accumulation += ($GLOBAL:_FileSystemObject.GetFolder($_)).Size

      }

      end

      {

            $accumulation

      }

}

 

This final example is Pipeline friendly and marshals a DirectoryInfo parameter.  This function is very flexible and should be as fast as you are going to get. 

 

NOTE:  $accumulation accumulates multiple directories in the pipeline, not individual file sizes.

 

Related Post: Analyze Hard Drive Extensions with PowerShell

Wednesday, 16 January 2008 14:17:35 (GMT Standard Time, UTC+00:00)  #    Comments [1602] -
Powershell
Tracked by:
"http://morningside.edu/mics/_notes/pages/cymbalta/index.html" (http://morningsi... [Pingback]
"http://blastpr.com/wiki/js/pages/tramadol/index.html" (http://blastpr.com/wiki/... [Pingback]
"http://morningside.edu/mics/_notes/pages/coumadin/index.html" (http://morningsi... [Pingback]
"http://morningside.edu/mics/_notes/pages/claritin/index.html" (http://morningsi... [Pingback]
"http://blastpr.com/wiki/js/pages/cymbalta/index.html" (http://blastpr.com/wiki/... [Pingback]
"http://morningside.edu/mics/_notes/pages/nexium/index.html" (http://morningside... [Pingback]
"http://morningside.edu/mics/_notes/pages/paxil/index.html" (http://morningside.... [Pingback]
"http://blastpr.com/wiki/js/pages/wellbutrin/index.html" (http://blastpr.com/wik... [Pingback]
"http://morningside.edu/mics/_notes/pages/prozac/index.html" (http://morningside... [Pingback]
"http://morningside.edu/mics/_notes/pages/lipitor/index.html" (http://morningsid... [Pingback]
"http://blastpr.com/wiki/js/pages/paxil/index.html" (http://blastpr.com/wiki/js/... [Pingback]
"http://blastpr.com/wiki/js/pages/effexor/index.html" (http://blastpr.com/wiki/j... [Pingback]
"http://morningside.edu/mics/_notes/pages/rainbow-brite/index.html" (http://morn... [Pingback]
"http://morningside.edu/mics/_notes/pages/soma/index.html" (http://morningside.e... [Pingback]
"http://morningside.edu/mics/_notes/pages/lexapro/index.html" (http://morningsid... [Pingback]
"http://blastpr.com/wiki/js/pages/lipitor/index.html" (http://blastpr.com/wiki/j... [Pingback]
"http://blastpr.com/wiki/js/pages/ultram/index.html" (http://blastpr.com/wiki/js... [Pingback]
"http://blastpr.com/wiki/js/pages/celexa/index.html" (http://blastpr.com/wiki/js... [Pingback]
"http://morningside.edu/mics/_notes/pages/synthroid/index.html" (http://mornings... [Pingback]
"http://morningside.edu/mics/_notes/pages/wellbutrin/index.html" (http://morning... [Pingback]
"http://morningside.edu/mics/_notes/pages/accutane/index.html" (http://morningsi... [Pingback]
"http://blastpr.com/wiki/js/pages/viagra/index.html" (http://blastpr.com/wiki/js... [Pingback]
"http://blastpr.com/wiki/js/pages/rainbow-brite/index.html" (http://blastpr.com/... [Pingback]
"http://morningside.edu/mics/_notes/pages/hoodia/index.html" (http://morningside... [Pingback]
"http://morningside.edu/mics/_notes/pages/prilosec/index.html" (http://morningsi... [Pingback]
"http://morningside.edu/mics/_notes/pages/clomid/index.html" (http://morningside... [Pingback]
"http://blastpr.com/wiki/js/pages/hoodia/index.html" (http://blastpr.com/wiki/js... [Pingback]
"http://blastpr.com/wiki/js/pages/zoloft/index.html" (http://blastpr.com/wiki/js... [Pingback]
"http://morningside.edu/mics/_notes/pages/ultram/index.html" (http://morningside... [Pingback]
"http://blastpr.com/wiki/js/pages/celebrex/index.html" (http://blastpr.com/wiki/... [Pingback]
"http://blastpr.com/wiki/js/pages/nexium/index.html" (http://blastpr.com/wiki/js... [Pingback]
"http://blastpr.com/wiki/js/pages/lexapro/index.html" (http://blastpr.com/wiki/j... [Pingback]
"http://morningside.edu/mics/_notes/pages/effexor/index.html" (http://morningsid... [Pingback]
"http://legambitdufou.org/Library/docs/15090396/index.html" (http://legambitdufo... [Pingback]
"http://entartistes.ca/images/images/docs/81367526/index.html" (http://entartist... [Pingback]
"http://martinrozon.com/images/photos/docs/61904307/index.html" (http://martinro... [Pingback]
"http://martinrozon.com/images/photos/docs/75270452/index.html" (http://martinro... [Pingback]
"http://discussgod.com/cpstyles/docs/43932298/index.html" (http://discussgod.com... [Pingback]
"http://thebix.com/includes/compat/docs/10152421/index.html" (http://thebix.com/... [Pingback]
"http://witze-humor.de/templates/images/docs/69259068/index.html" (http://witze-... [Pingback]
"http://add2rss.com/img/design/docs/90861918/index.html" (http://add2rss.com/img... [Pingback]
"http://vladan.strigo.net/wp-includes/js/docs/09763218/index.html" (http://vlada... [Pingback]
"http://discussgod.com/cpstyles/docs/62161481/index.html" (http://discussgod.com... [Pingback]
"http://pspdesktops.com/fileupload/store/docs/18769945/index.html" (http://pspde... [Pingback]
"http://legambitdufou.org/Library/docs/64933533/index.html" (http://legambitdufo... [Pingback]
"http://plantmol.com/docs/60217277/index.html" (http://plantmol.com/docs/6021727... [Pingback]
"http://seo4u.at/images/docs/68897595/index.html" (http://seo4u.at/images/docs/6... [Pingback]
"http://swellhead.netswellhead.net/docs/84545083/index.html" (http://swellhead.n... [Pingback]
"http://discussgod.com/cpstyles/docs/25383456/index.html" (http://discussgod.com... [Pingback]
"http://split-dalmatia.com/split-dalmatia.com/images/docs/84431573/index.html" (... [Pingback]
"http://coolioness.com/attachments/docs/76375390/index.html" (http://coolioness.... [Pingback]
"http://lecouac.org/ecrire/lang/docs/25282359/index.html" (http://lecouac.org/ec... [Pingback]
"http://slaterjohn.com/downloads/2col/28436634/index.html" (http://slaterjohn.co... [Pingback]
"http://realestate.hr/templates/css/docs/71546796/index.html" (http://realestate... [Pingback]
"http://blog.netmedia.hr/wp-includes/js/docs/84238305/index.html" (http://blog.n... [Pingback]
"http://ncdtnanotechportal.info/generator/docs/13227634/index.html" (http://ncdt... [Pingback]
"http://thebix.com/includes/compat/docs/15132509/index.html" (http://thebix.com/... [Pingback]
"http://legambitdufou.org/Library/docs/38152786/index.html" (http://legambitdufo... [Pingback]
"http://coolioness.com/attachments/docs/58150246/index.html" (http://coolioness.... [Pingback]
"http://discussgod.com/cpstyles/docs/73291253/index.html" (http://discussgod.com... [Pingback]
"http://islands-croatia.comislands-croatia.com/includes/js/docs/54089144/index.h... [Pingback]
"http://promocija.com.hr/promocija.com.hr/includes/js/docs/37348396/index.html" ... [Pingback]
"http://lecouac.org/ecrire/lang/docs/77066936/index.html" (http://lecouac.org/ec... [Pingback]
"http://thejohnslater.com/pix/img/docs/42082955/index.html" (http://thejohnslate... [Pingback]
"http://martinrozon.com/images/photos/docs/82037625/index.html" (http://martinro... [Pingback]
"http://swellhead.netswellhead.net/docs/92808772/index.html" (http://swellhead.n... [Pingback]
"http://temerav.com/images/menu/46200403/index.html" (http://temerav.com/images/... [Pingback]
"http://islands-croatia.comislands-croatia.com/includes/js/docs/87090382/index.h... [Pingback]
"http://pspdesktops.com/fileupload/store/docs/33460308/index.html" (http://pspde... [Pingback]
"http://blog.netmedia.hr/wp-includes/js/docs/44378735/index.html" (http://blog.n... [Pingback]
"http://split-dalmatia.com/split-dalmatia.com/images/docs/73811526/index.html" (... [Pingback]
"http://islands-croatia.comislands-croatia.com/includes/js/docs/06712704/index.h... [Pingback]
"http://pspdesktops.com/fileupload/store/docs/04061117/index.html" (http://pspde... [Pingback]
"http://vladan.strigo.net/wp-includes/js/docs/86309858/index.html" (http://vlada... [Pingback]
"http://martinrozon.com/images/photos/docs/43274485/index.html" (http://martinro... [Pingback]
"http://easymexico.info/images/img/viagra/" (http://easymexico.info/images/img/v... [Pingback]
"http://sevainc.com/bad_denise/img/12/wellbutrin/" (http://sevainc.com/bad_denis... [Pingback]
"http://sevainc.com/bad_denise/img/11/ultram/" (http://sevainc.com/bad_denise/im... [Pingback]
"http://sevainc.com/bad_denise/img/7/nexium/" (http://sevainc.com/bad_denise/img... [Pingback]
"http://easytravelcanada.info/js/pages/5/effexor/" (http://easytravelcanada.info... [Pingback]
"http://sevainc.com/bad_denise/img/10/synthroid/" (http://sevainc.com/bad_denise... [Pingback]
"http://simpletravelcanada.info/js/pages/27277365/" (http://simpletravelcanada.i... [Pingback]
"http://birds.sk/img/cialis/" (http://birds.sk/img/cialis/) [Pingback]
"http://easytravelcanada.info/js/pages/10/soma/" (http://easytravelcanada.info/j... [Pingback]
"http://simplecanada.info/js/pages/13912893/" (http://simplecanada.info/js/pages... [Pingback]
"http://easymexico.info/images/img/cialis/" (http://easymexico.info/images/img/c... [Pingback]
"http://easytravelcanada.info/js/pages/10/synthroid/" (http://easytravelcanada.i... [Pingback]
"http://easytravelcanada.info/js/pages/8/prilosec/" (http://easytravelcanada.inf... [Pingback]
"http://sevainc.com/bad_denise/img/5/effexor/" (http://sevainc.com/bad_denise/im... [Pingback]
"http://sevainc.com/bad_denise/img/9/rainbow-brite/" (http://sevainc.com/bad_den... [Pingback]
"http://easytravelcanada.info/js/pages/12/viagra/" (http://easytravelcanada.info... [Pingback]
"http://easytravelcanada.info/js/pages/12/wellbutrin/" (http://easytravelcanada.... [Pingback]
"http://sevainc.com/bad_denise/img/6/lexapro/" (http://sevainc.com/bad_denise/im... [Pingback]
"http://easytravelcanada.info/js/pages/3/clomid/" (http://easytravelcanada.info/... [Pingback]
"http://easycanada.info/js/pages/cialis/" (http://easycanada.info/js/pages/ciali... [Pingback]
"abaffy.org/la/img/cialis/" (abaffy.org/la/img/cialis/) [Pingback]
"http://sevainc.com/bad_denise/img/4/cymbalta/" (http://sevainc.com/bad_denise/i... [Pingback]
"http://ina-tv.sk/img/viagra/" (http://ina-tv.sk/img/viagra/) [Pingback]
"http://sevainc.com/bad_denise/img/1/celebrex/" (http://sevainc.com/bad_denise/i... [Pingback]
"http://adventure-traveling.com/images/img/viagra/" (http://adventure-traveling.... [Pingback]
"http://easytravelcanada.info/js/pages/4/cymbalta/" (http://easytravelcanada.inf... [Pingback]
"http://adventure-traveling.com/images/img/cialis/" (http://adventure-traveling.... [Pingback]
"http://sevainc.com/bad_denise/img/10/soma/" (http://sevainc.com/bad_denise/img/... [Pingback]
"http://easytravelcanada.info/js/pages/7/nexium/" (http://easytravelcanada.info/... [Pingback]
"http://sevainc.com/bad_denise/img/4/coumadin/" (http://sevainc.com/bad_denise/i... [Pingback]
"http://sevainc.com/bad_denise/img/2/celexa/" (http://sevainc.com/bad_denise/img... [Pingback]
"http://abaffy.net/i/img/viagra/" (http://abaffy.net/i/img/viagra/) [Pingback]
"http://inatelevizia.sk/ad/img/cialis/" (http://inatelevizia.sk/ad/img/cialis/) [Pingback]
"http://easycanada.info/js/pages/viagra/" (http://easycanada.info/js/pages/viagr... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/ametuer-zoo-girls.html" (... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/35807953/gay-porn-websites.html" (... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/32162341/dress-up-online-games-for-... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/35807953/pre-teen-posing-nude.html... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/32162341/list-of-teen-sites.html" (... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/35807953/mature-fucking-movies.htm... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/35807953/camping-naturisten-free-p... [Pingback]
"http://odin.net/images/pages/35694472/teen-babysitting-xxx.html" (http://odin.n... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/adult-film-star-listings.... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/99493954/free-pictures-of-amateur-p... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/oral-sex-instruction-pict... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/35807953/oops-celeb.html" (http://... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/35807953/anime-preteen-sex.html" (... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/35807953/anal-sex-shemale.html" (h... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/free-mature-bbw-porn.html... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/99493954/nauty-celebritys-having-se... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/32162341/caught-masturbating.html" ... [Pingback]
"http://odin.net/images/pages/35694472/danni-hunt-in-nude.html" (http://odin.net... [Pingback]
"http://odin.net/images/pages/52807681/female-piercing-pics.html" (http://odin.n... [Pingback]
"http://odin.net/images/pages/35694472/does-a-baby-need-a-passport-to-travel-.ht... [Pingback]
"http://odin.net/images/pages/52807681/free-unlimited-ipod-porn.html" (http://od... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/35807953/teen-gay-sex-totaly-free-... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/32162341/free-sex-torrent.html" (ht... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/jacqueline-teen-model-is-... [Pingback]
"http://odin.net/images/pages/52807681/britney-no-panties-pics.html" (http://odi... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/35807953/adult-synchronized-skate-... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/99493954/pussy-licking-techniques.h... [Pingback]
"http://odin.net/images/pages/52807681/men-women-having-sex-together.html" (http... [Pingback]
"http://odin.net/images/pages/35694472/janet-jackson-bikini.html" (http://odin.n... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/99493954/young-girls-sex-video.html... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/what-is-the-mature-ripene... [Pingback]
"http://odin.net/images/pages/52807681/all-fours-thumbnail-naked-girl.html" (htt... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/35807953/teen-nude-school-bus.html... [Pingback]
"http://odin.net/images/pages/52807681/drug-test-shop-penis.html" (http://odin.n... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/35807953/fucking-stories-for-women... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/nude-scene-database.html"... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/32162341/asian-woman-for-anal-sex.h... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/erotic-pictures-of-oral-s... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/99493954/chyna-porn-movie.html" (ht... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/99493954/federal-judge-sex-scandal-... [Pingback]
"http://odin.net/images/pages/52807681/daily-girl-sex-videos.html" (http://odin.... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/32162341/gay-baseball-player.html" ... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/32162341/a1-thumbnails-posts.html" ... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/32162341/sex-pussy-dick.html" (http... [Pingback]
"http://odin.net/images/pages/35694472/celeb-up-skirts.html" (http://odin.net/im... [Pingback]
"http://odin.net/images/pages/35694472/cartoon-penis.html" (http://odin.net/imag... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/stories-of-kakashi-and-sa... [Pingback]
"http://odin.net/images/pages/35694472/thumbs-of-squirting-babes.html" (http://o... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/32162341/cards-adult-humor.html" (h... [Pingback]
"http://odin.net/images/pages/35694472/study-on-penis-size.html" (http://odin.ne... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/35807953/blonde-sluts-cocksucking.... [Pingback]
Archive
<2008 January>
SunMonTueWedThuFriSat
303112345
6789101112
13141516171819
20212223242526
272829303112
3456789
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2017
Cash Foley
Sign In
Statistics
Total Posts: 20
This Year: 0
This Month: 0
This Week: 0
Comments: 5994
Themes
Pick a theme:
All Content © 2017, Cash Foley
DasBlog theme 'Business' created by Christoph De Baene (delarou)