Calculating File Size Stats Using Measure-Object

Measure-Object is a very useful function that provides basic aggregate functions similar to the ones found in SQL.  For instance, I wanted to know some basic stats on my music files.  Perfect job for PowerShell and Measure-Object.  Here’s what I executed:
 
PS:24 # gci "$home\my documents\my music" -rec |
>>      where {-not $_.MshIsContainer -and
>>             $_.Extension -match "wma|mp3"} |
>>      Measure-Object -property length -sum -min -max -ave
>>
Count    : 1018
Average  : 2991362.43123772
Sum      : 3045206955
Max      : 102750941
Min      : 89025
Property : Length
 
Pretty simple and in the process of doing this I discoverd I had a music of size 102,750,941 bytes.  Yikes!  Finding that large file was pretty simple using PS:

PS:28 # gci "$home\my documents\my music" -rec |
>>      where {-not $_.MshIsContainer -and
>>      $_.Extension -match "wma|mp3" -and $_.Length -gt 95M}
>>
    Directory: FileSystem::C:\Documents and Settings\Keith\my documents\
    my music\Jonathan Goldman\Dolphin Dreams
Mode                LastWriteTime     Length Name
—-                ————-     —— —-
-a—         8/17/2004   9:10 PM  102750941 01 Dolphin Dreams- A Sonic
                                                Environment.wma
 
File deleted!  Now let’s see how that affects the stats.  Notice the max file size is now ~15 MB.  That’s better.
 

PS:31 # gci "$home\my documents\my music" -rec |
>>      where {-not $_.MshIsContainer -and
>>             $_.Extension -match "wma|mp3"} |
>>      Measure-Object -property length -sum -min -max -ave
>>
 

Count    : 1017
Average  : 2893270.41691249
Sum      : 2942456014
Max      : 15711351
Min      : 89025
Property : Length

Advertisements
This entry was posted in PowerShell. Bookmark the permalink.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s