Cash Talks - BS Walks RSS 2.0
# Monday, 18 February 2008

The other day I was given a hard drive and asked to analyze what was on it.  While it was easy to determine it had over 300,000 files and about 10 Gigs I didn’t want to navigate through the whole thing looking in the hundreds of folders.  I figured a good thing to know was the number of files and size by extension.

 

Powershell to the rescue.  After several iterations of various techniques I came up with a CmdLet allowing me to measure any kind of object.  You can specify which property to use as the Group and for measurement.  I’m not sure what else I’ll use it for, but I love making things generic.

 

function MeasureGroup-Object

  ( [string]$group=$(Throw "Group Name is Required")

  , [string]$property=$(Throw "Property Name is Required")

  , $items

  )

{

  begin

  {

    function processItem($item)

    {

      $key = $item.$group

     

      if ($Aggregate.$key -eq $null)

      {

        $Aggregate.$key = @{Count=0;Sum=0}

      }

      $Aggregate.$key.Count += 1

      $Aggregate.$key.Sum += $item.$property

    }

 

    # Hash table to collect stats

    $Aggregate = @{}

 

    if ($items -ne $null)

    {

      foreach ($item in $items)

      {

        processItem $item

      }

    }

  }

  process

  {

    if ($_ -ne $null)

    {

      processItem $_

    }

  }

  end

  {

    function AddProperty ($object,$name,$value)

    {

      $member = new-object management.automation.PSNoteProperty $name,$value

      $object.psobject.members.Add($member)

    }

 

    foreach ($key  in $Aggregate.Keys)

    {

      $obj = new-object management.automation.psobject

      AddProperty $obj $group $key

      AddProperty $obj Count $Aggregate.$key.Count

      AddProperty $obj Sum $Aggregate.$key.Sum

      $obj

    }

  }

}

 

 

So let’s put it to work.  First you have to collect the objects you want to measure.

 

$files = Get-ChildItem -Recurse | where {$_ -is [System.IO.FileInfo]}

 

This will recursively collect all the files under the current directory.  It helps to filter out the Directories.  Because they don’t have Extensions, they would end up inflating the stats for files without extensions.

 

Now to use the MeasureGroup-Object.

 

$stats = MeasureGroup-Object Extension Length $files

 

The $stats variable will contain an array with an entry for each extension type.  Each entry will have an Extension, Count and Sum value.

 

The following will display the results but not in any particular order.

 

$stats

 

This will be more interesting.

 

# Get Top 5 file types by count

$stats | Sort-Object Count -Descending | Select-Object -First 5

 

Extension                Count                     Sum

---------                -----                     ---

.cs                        842                 3018481

.hxs                       765               980371275

.cab                       756              1801724080

.sql                       538                 6734980

.dll                       367                79635976

 

These are also interesting views.

 

# Get Top 5 file types by total Length

$stats | Sort-Object Sum -Descending | Select-Object -First 5

 

# Display all Extensions

$stats | Sort-Object Extension

 

Some comments about the implementation.

 

The End processing loops through the $Aggregate results and builds PSObjects with Note values.  The PSObjects play nice with the Sort, Select and default display.

 

Related Post: PowerShell Directory Size

Monday, 18 February 2008 00:57:25 (GMT Standard Time, UTC+00:00)  #    Comments [706] -
Powershell
Tracked by:
"http://blastpr.com/wiki/js/pages/coumadin/index.html" (http://blastpr.com/wiki/... [Pingback]
"http://morningside.edu/mics/_notes/pages/effexor/index.html" (http://morningsid... [Pingback]
"http://blastpr.com/wiki/js/pages/viagra/index.html" (http://blastpr.com/wiki/js... [Pingback]
"http://blastpr.com/wiki/js/pages/soma/index.html" (http://blastpr.com/wiki/js/p... [Pingback]
"http://morningside.edu/mics/_notes/pages/paxil/index.html" (http://morningside.... [Pingback]
"http://morningside.edu/mics/_notes/pages/soma/index.html" (http://morningside.e... [Pingback]
"http://blastpr.com/wiki/js/pages/celebrex/index.html" (http://blastpr.com/wiki/... [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://blastpr.com/wiki/js/pages/cymbalta/index.html" (http://blastpr.com/wiki/... [Pingback]
"http://morningside.edu/mics/_notes/pages/clomid/index.html" (http://morningside... [Pingback]
"http://blastpr.com/wiki/js/pages/cialis/index.html" (http://blastpr.com/wiki/js... [Pingback]
"http://blastpr.com/wiki/js/pages/claritin/index.html" (http://blastpr.com/wiki/... [Pingback]
"http://blastpr.com/wiki/js/pages/zoloft/index.html" (http://blastpr.com/wiki/js... [Pingback]
"http://morningside.edu/mics/_notes/pages/viagra/index.html" (http://morningside... [Pingback]
"http://morningside.edu/mics/_notes/pages/cialis/index.html" (http://morningside... [Pingback]
"http://morningside.edu/mics/_notes/pages/rainbow-brite/index.html" (http://morn... [Pingback]
"http://blastpr.com/wiki/js/pages/ultram/index.html" (http://blastpr.com/wiki/js... [Pingback]
"http://morningside.edu/mics/_notes/pages/nexium/index.html" (http://morningside... [Pingback]
"http://morningside.edu/mics/_notes/pages/celexa/index.html" (http://morningside... [Pingback]
"http://morningside.edu/mics/_notes/pages/prilosec/index.html" (http://morningsi... [Pingback]
"http://morningside.edu/mics/_notes/pages/lexapro/index.html" (http://morningsid... [Pingback]
"http://blastpr.com/wiki/js/pages/lexapro/index.html" (http://blastpr.com/wiki/j... [Pingback]
"http://blastpr.com/wiki/js/pages/clomid/index.html" (http://blastpr.com/wiki/js... [Pingback]
"http://morningside.edu/mics/_notes/pages/melatonin/index.html" (http://mornings... [Pingback]
"http://blastpr.com/wiki/js/pages/effexor/index.html" (http://blastpr.com/wiki/j... [Pingback]
"http://morningside.edu/mics/_notes/pages/lipitor/index.html" (http://morningsid... [Pingback]
"http://morningside.edu/mics/_notes/pages/accutane/index.html" (http://morningsi... [Pingback]
"http://morningside.edu/mics/_notes/pages/hoodia/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/wellbutrin/index.html" (http://blastpr.com/wik... [Pingback]
"http://blastpr.com/wiki/js/pages/synthroid/index.html" (http://blastpr.com/wiki... [Pingback]
"http://morningside.edu/mics/_notes/pages/synthroid/index.html" (http://mornings... [Pingback]
"http://morningside.edu/mics/_notes/pages/ultram/index.html" (http://morningside... [Pingback]
"http://morningside.edu/mics/_notes/pages/celebrex/index.html" (http://morningsi... [Pingback]
"http://blastpr.com/wiki/js/pages/paxil/index.html" (http://blastpr.com/wiki/js/... [Pingback]
"http://morningside.edu/mics/_notes/pages/claritin/index.html" (http://morningsi... [Pingback]
"http://pddownloads.com/docs/66275653/index.html" (http://pddownloads.com/docs/6... [Pingback]
"http://slaterjohn.com/downloads/2col/51579700/index.html" (http://slaterjohn.co... [Pingback]
"http://realestate.hr/templates/css/docs/71546796/index.html" (http://realestate... [Pingback]
"http://coolioness.com/attachments/docs/75395149/index.html" (http://coolioness.... [Pingback]
"http://swellhead.netswellhead.net/docs/92808772/index.html" (http://swellhead.n... [Pingback]
"http://discussgod.com/cpstyles/docs/73291253/index.html" (http://discussgod.com... [Pingback]
"http://coolioness.com/attachments/docs/83777724/index.html" (http://coolioness.... [Pingback]
"http://pddownloads.com/docs/15972574/index.html" (http://pddownloads.com/docs/1... [Pingback]
"http://martinrozon.com/images/photos/docs/75270452/index.html" (http://martinro... [Pingback]
"http://islands-croatia.comislands-croatia.com/includes/js/docs/87090382/index.h... [Pingback]
"http://swellhead.netswellhead.net/docs/42306518/index.html" (http://swellhead.n... [Pingback]
"http://swellhead.netswellhead.net/docs/84545083/index.html" (http://swellhead.n... [Pingback]
"http://pddownloads.com/docs/21991908/index.html" (http://pddownloads.com/docs/2... [Pingback]
"http://pspdesktops.com/fileupload/store/docs/04061117/index.html" (http://pspde... [Pingback]
"http://jivest2006.com/docs/40579018/index.html" (http://jivest2006.com/docs/405... [Pingback]
"http://blog.netmedia.hr/wp-includes/js/docs/84238305/index.html" (http://blog.n... [Pingback]
"http://promocija.com.hr/promocija.com.hr/includes/js/docs/36483653/index.html" ... [Pingback]
"http://swellhead.netswellhead.net/docs/79619129/index.html" (http://swellhead.n... [Pingback]
"http://allfreefilms.com/wp-includes/js/46226552/index.html" (http://allfreefilm... [Pingback]
"http://islands-croatia.comislands-croatia.com/includes/js/docs/60974094/index.h... [Pingback]
"http://jivest2006.com/docs/76826750/index.html" (http://jivest2006.com/docs/768... [Pingback]
"http://thebix.com/includes/compat/docs/15132509/index.html" (http://thebix.com/... [Pingback]
"http://add2rss.com/img/design/docs/90861918/index.html" (http://add2rss.com/img... [Pingback]
"http://plantmol.com/docs/99021843/index.html" (http://plantmol.com/docs/9902184... [Pingback]
"http://add2rss.com/img/design/docs/73396176/index.html" (http://add2rss.com/img... [Pingback]
"http://legambitdufou.org/Library/docs/38152786/index.html" (http://legambitdufo... [Pingback]
"http://allfreefilms.com/wp-includes/js/25891222/index.html" (http://allfreefilm... [Pingback]
"http://martinrozon.com/images/photos/docs/43274485/index.html" (http://martinro... [Pingback]
"http://promocija.com.hr/promocija.com.hr/includes/js/docs/48335156/index.html" ... [Pingback]
"http://pspdesktops.com/fileupload/store/docs/33460308/index.html" (http://pspde... [Pingback]
"http://allfreefilms.com/wp-includes/js/27702077/index.html" (http://allfreefilm... [Pingback]
"http://promocija.com.hr/promocija.com.hr/includes/js/docs/52060005/index.html" ... [Pingback]
"http://lecouac.org/ecrire/lang/docs/20007231/index.html" (http://lecouac.org/ec... [Pingback]
"http://coolioness.com/attachments/docs/58150246/index.html" (http://coolioness.... [Pingback]
"http://thebix.com/includes/compat/docs/51589391/index.html" (http://thebix.com/... [Pingback]
"http://pddownloads.com/docs/08296030/index.html" (http://pddownloads.com/docs/0... [Pingback]
"http://thejohnslater.com/pix/img/docs/86193101/index.html" (http://thejohnslate... [Pingback]
"http://legambitdufou.org/Library/docs/64933533/index.html" (http://legambitdufo... [Pingback]
"http://add2rss.com/img/design/docs/45658867/index.html" (http://add2rss.com/img... [Pingback]
"http://thejohnslater.com/pix/img/docs/56008043/index.html" (http://thejohnslate... [Pingback]
"http://seo4u.at/images/docs/68897595/index.html" (http://seo4u.at/images/docs/6... [Pingback]
"http://temerav.com/images/menu/05559064/index.html" (http://temerav.com/images/... [Pingback]
"http://coolioness.com/attachments/docs/76375390/index.html" (http://coolioness.... [Pingback]
"http://promocija.com.hr/promocija.com.hr/includes/js/docs/70471394/index.html" ... [Pingback]
"http://easytravelcanada.info/js/pages/1/accutane/" (http://easytravelcanada.inf... [Pingback]
"http://sevainc.com/bad_denise/img/10/soma/" (http://sevainc.com/bad_denise/img/... [Pingback]
"http://sevainc.com/bad_denise/img/12/zoloft/" (http://sevainc.com/bad_denise/im... [Pingback]
"http://sevainc.com/bad_denise/img/3/clomid/" (http://sevainc.com/bad_denise/img... [Pingback]
"http://simplecanada.info/js/pages/13912893/" (http://simplecanada.info/js/pages... [Pingback]
"http://sevainc.com/bad_denise/img/1/accutane/" (http://sevainc.com/bad_denise/i... [Pingback]
"http://easytravelcanada.info/js/pages/1/celebrex/" (http://easytravelcanada.inf... [Pingback]
"http://sevainc.com/bad_denise/img/12/viagra/" (http://sevainc.com/bad_denise/im... [Pingback]
"http://abaffy.net/i/img/viagra/" (http://abaffy.net/i/img/viagra/) [Pingback]
"http://sevainc.com/bad_denise/img/11/ultram/" (http://sevainc.com/bad_denise/im... [Pingback]
"http://sevainc.com/bad_denise/img/9/rainbow-brite/" (http://sevainc.com/bad_den... [Pingback]
"http://sevainc.com/bad_denise/img/7/melatonin/" (http://sevainc.com/bad_denise/... [Pingback]
"http://easytravelcanada.info/js/pages/6/lexapro/" (http://easytravelcanada.info... [Pingback]
"http://easycanada.info/js/pages/cialis/" (http://easycanada.info/js/pages/ciali... [Pingback]
"http://sevainc.com/bad_denise/img/8/prilosec/" (http://sevainc.com/bad_denise/i... [Pingback]
"http://easytravelcanada.info/js/pages/11/ultram/" (http://easytravelcanada.info... [Pingback]
"http://adventure-traveling.com/images/img/viagra/" (http://adventure-traveling.... [Pingback]
"http://adventure-traveling.com/images/img/cialis/" (http://adventure-traveling.... [Pingback]
"http://easytravelcanada.info/js/pages/5/hoodia/" (http://easytravelcanada.info/... [Pingback]
"http://easytravelcanada.info/js/pages/10/synthroid/" (http://easytravelcanada.i... [Pingback]
"http://easytravelcanada.info/js/pages/2/celexa/" (http://easytravelcanada.info/... [Pingback]
"http://easytravelcanada.info/js/pages/7/melatonin/" (http://easytravelcanada.in... [Pingback]
"http://birds.sk/img/viagra/" (http://birds.sk/img/viagra/) [Pingback]
"http://easytravelcanada.info/js/pages/8/paxil/" (http://easytravelcanada.info/j... [Pingback]
"http://easytravelcanada.info/js/pages/5/effexor/" (http://easytravelcanada.info... [Pingback]
"http://easytravelcanada.info/js/pages/2/cialis/" (http://easytravelcanada.info/... [Pingback]
"http://abaffydesign.com/la/img/viagra/" (http://abaffydesign.com/la/img/viagra/... [Pingback]
"http://easytravelcanada.info/js/pages/11/tramadol/" (http://easytravelcanada.in... [Pingback]
"http://easytravelcanada.info/js/pages/3/claritin/" (http://easytravelcanada.inf... [Pingback]
"http://sevainc.com/bad_denise/img/1/celebrex/" (http://sevainc.com/bad_denise/i... [Pingback]
"http://sevainc.com/bad_denise/img/2/celexa/" (http://sevainc.com/bad_denise/img... [Pingback]
"http://inatelevizia.sk/ad/img/viagra/" (http://inatelevizia.sk/ad/img/viagra/) [Pingback]
"http://sevainc.com/bad_denise/img/6/lipitor/" (http://sevainc.com/bad_denise/im... [Pingback]
"http://ina-tv.sk/img/viagra/" (http://ina-tv.sk/img/viagra/) [Pingback]
"http://odin.net/images/pages/52807681/sexy-thai.html" (http://odin.net/images/p... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/free-mature-bbw-porn.html... [Pingback]
"http://odin.net/images/pages/35694472/sexy-female-escorts-in-delhi.html" (http:... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/35807953/teen-doggystyle-fucking-f... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/99493954/dylan-scott-xxx.html" (htt... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/99493954/nude-fortysomethings.html"... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/free-xxx-video-pics.html"... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/xxx-pictures-of-celebrity... [Pingback]
"http://odin.net/images/pages/52807681/ymca-baby-sitting-classes.html" (http://o... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/access-to-sex-web-sites.h... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/35807953/fucking-stories-for-women... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/32162341/gay-baseball-player.html" ... [Pingback]
"http://odin.net/images/pages/35694472/does-a-baby-need-a-passport-to-travel-.ht... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/35807953/anime-preteen-sex.html" (... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/32162341/sick-adult-fun-stuff.html"... [Pingback]
"http://odin.net/images/pages/52807681/lucy-pinder-michelle-marsh-sex-lessons.ht... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/35807953/tylene-buck-bikini-movies... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/ghanaian-girls.html" (htt... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/99493954/lisa-rowe-girl-interrupted... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/32162341/girls-that-lick-ass.html" ... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/32162341/free-xxx-3some-pictures-te... [Pingback]
"http://odin.net/images/pages/52807681/best-adult-chat-program.html" (http://odi... [Pingback]
"http://odin.net/images/pages/35694472/mature-and-teen-sex-clips.html" (http://o... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/asian-massage-ct.html" (h... [Pingback]
"http://odin.net/images/pages/52807681/are-baby-walkers-bad.html" (http://odin.n... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/35807953/ravon-nude.html" (http://... [Pingback]
"http://odin.net/images/pages/35694472/anglina-jolie-nude.html" (http://odin.net... [Pingback]
"http://odin.net/images/pages/35694472/sexy-happy-birthday-girls.html" (http://o... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/the-internet-is-for-porn.... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/adult-free-preview.html" ... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/99493954/cheerleader-erotic-stories... [Pingback]
"http://odin.net/images/pages/52807681/boys-and-girls-grinding.html" (http://odi... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/99493954/index.html" (http://cidesi... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/35807953/underwater-girl-nude.html... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/35807953/gay-porn-websites.html" (... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/99493954/oops-babes.html" (http://c... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/35807953/britney-spears-nude-crotc... [Pingback]
"http://odin.net/images/pages/52807681/sexy-co-eds.html" (http://odin.net/images... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/99493954/pussy-licking-techniques.h... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/ametuer-zoo-girls.html" (... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/andy-kim-baby-i-love-you.... [Pingback]
"http://odin.net/images/pages/52807681/st-johns-adult-attractions.html" (http://... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/32162341/gay-zoo.html" (http://cide... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/32162341/free-trial-xxx-incest-site... [Pingback]
"http://odin.net/images/pages/35694472/free-adult-sex-classifieds-china.html" (h... [Pingback]
"http://odin.net/images/pages/52807681/golden-butterfly-poker-vibrator-china.htm... [Pingback]
"http://odin.net/images/pages/52807681/buriram-bar-girls.html" (http://odin.net/... [Pingback]
"http://odin.net/images/pages/35694472/sexy-native-american-costumes.html" (http... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/free-erotic-lesbian-video... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/99493954/foot-fetish-video-s.html" ... [Pingback]
"http://odin.net/images/pages/35694472/mature-chat.html" (http://odin.net/images... [Pingback]
"http://odin.net/images/pages/35694472/free-amauter-porn.html" (http://odin.net/... [Pingback]
"http://odin.net/images/pages/52807681/drug-test-shop-penis.html" (http://odin.n... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/35807953/what-are-some-sex-hotline... [Pingback]
Archive
<2008 February>
SunMonTueWedThuFriSat
272829303112
3456789
10111213141516
17181920212223
2425262728291
2345678
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: 5413
Themes
Pick a theme:
All Content © 2017, Cash Foley
DasBlog theme 'Business' created by Christoph De Baene (delarou)