Archive for July 16th, 2008

Measure-Latest: finding latest date/time

Today I needed to find the latest date/time value from a set of values and stumbled upon an issue with the standard measure-object cmdlet failing for DateTime values (click the link to vote for this to be fixed by the way).

Obviously, as a true PowerSheller, whenever I come across a limitation like that I simply use the extensibility of PowerShell to get through. Here’s the Measure-Latest function which I created. It takes the values from the pipeline and outputs the latest of them (or $null if the pipeline was empty or only had $null values):

# get a set of DateTime values from the pipeline
# filter out $nulls and produce the latest of them
# (c) Dmitry Sotnikov
function Measure-Latest {
    BEGIN { $latest = $null }
    PROCESS {
            if (($_ -ne $null) -and (($latest -eq $null) -or ($_ -gt $latest))) {
                $latest = $_ 
            }
    }
    END { $latest }
}

Usage is pretty simple:

# when was the last time something was modified in the current folder?
dir | foreach { $_.LastWriteTime } | Measure-Latest

Enjoy!

Tags: , , , ,

Advertisement

Legal

The posts on this blog are provided “as is” with no warranties and confer no rights. The opinions expressed on this site are mine and mine alone, and do not necessarily represent those of my employer - WSO2 or anyone else for that matter. All trademarks acknowledged.

© 2007-2014 Dmitry Sotnikov

July 2008
M T W T F S S
 123456
78910111213
14151617181920
21222324252627
28293031  

%d bloggers like this: