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: Examples, KB, Knowledge Base, Known Issues, PowerShell