19

Get the last modified date and time

Before we dive into this week’s teaser we would like to announce the winner of last week’s challenge. Ioan Corcodel, congratulations! You take with you a copy of Microsoft Windows PowerShell 3.0 First Look written by Adam Driscoll. Once again, we would like to thank our sponsor Packt, one of the most prolific and fast-growing tech book publishers in the world, for providing such a cool prize.

Ioan, your solution worked great and it was 78 characters long. By the way, I was able to shave off a few more characters ūüôā ,75 vs. 78 :

echo Hannah Jeffrey ‘12321’ Madam Abracadabra|?{-join$_[$_.Length..0]-eq$_}

As for this week’s teaser, your task is to get the last modified date and time of a file system object, without referring to the LastWriteTime property.
For this example, you need to emit the LastWriteTime value of powershell.exe as a System.DateTime object.

One thing though, there’s no prize this week.¬†I know you guys are here for the challenge and the prize is only an excuse to participate and share your knowledge. ūüôā
Again, comments are allowed until Friday and we will announce the winner on Monday, next week.

Please use the comment box at the bottom of this page to submit your solution. Don’t have a solution of your own, or it has been already posted by others? You can still participate and add your voice by voting on a existing comments, use the up/down voting arrows.

Filed in: Brainteasers Tags: ,

19 Responses to "Get the last modified date and time"

  1. John Ludlow says:

    $file.LastWriteTimeUtc.ToLocalTime()

    ;P
    (Ok, going for LastWriteTimeUtc and converting to local, instead of LastWriteTime is probably a little cheeky given the rules of this challenge)

  2. Sahal says:

    [IO.File]::GetLastWriteTime(“$PSHomePowershell.exe”)

  3. (gi $PSHomePowershell.exe).get_LastWriteTime()

    • not sure what the starting point is, if I am allowed to show the date more than once (edit: probably not, because it returns collection not datetime, duh!), if I has to be able to get the result from host other than posh console. so here are few options:

      1) clean, freshly booted station, one posh console running (14 chars):
      ps *ll|gi|date

      2) runiing from console outputting more the result as many times as there are consoles runnning (21 chars)
      ps powershell|gi|date

      of course I can patch it further adding @()[0] etc. but it adds too much chars.

      3) assuming just the default installation of powershell with no modifications (21 chars)
      gi $pshome*l.e*|date

      4) assuming standard PowerShell installation on clean station (19 chars)

      gcm p*ll.e*|gi|date

  4. DVS says:

    cmd /c “dir $file /t:w”

  5. This doesn’t produce the [datettime] object on the output.

Leave a Reply

Submit Comment

© 2016 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: