Little Things in PowerShell – Determine Timespan of Files in a Dir

Often it is the "little things" that put a smile on your face.  PowerShell does that for me a lot.  Take for instance a simple little problem I ran into today while unit testing some code that generates log files.  I wanted to know how long it took to write all the log files to disk.  Due to the way the code works on a background thread, it wasn’t particularly easy to use a System.Diagnostics.Stopwatch to do this.  You could do this manually by inspecting the file dates (DateCreated and DateModified) in Windows Explorer but it only shows resolution down to minutes and I have to do the math myself.  No good.  Well this information is easy to figure out with PowerShell.  Just CD into the log dir and execute:

PS> $start = dir | foreach {$_.CreationTime} | sort | select -first 1
PS> $stop  = dir | foreach {$_.LastWriteTime} | sort -desc | select -first 1
PS> $stop – $start

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 10
Milliseconds      : 734
Ticks             : 107341689
TotalDays         : 0.000124238065972222
TotalHours        : 0.00298171358333333
TotalMinutes      : 0.178902815
TotalSeconds      : 10.7341689
TotalMilliseconds : 10734.1689


Yes PowerShell can do all sorts of acrobatics WRT managing and querying a computer but it can also do the simple stuff in a pretty straight forward manner.

This entry was posted in PowerShell. Bookmark the permalink.

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