Impractical One-liner Challenge #2

It’s back, your favorite Friday PowerShell diversion!

The get-date command has a useful method called GetDateTimeFormats() that lists all of the available date-time formats available on your system, suitable for you to find just the right date format to fit your needs.  However, actually using the output can be difficult, as you must use the following syntax, (Get-Date).GetDateTimeFormats()[<index>] and picking the right one from a huge non-numbered list can be challenging.

Your Mission

Display all of the dateTime formats available, along with the index position of each in a graphical GUI user interface for the user to pick the format they desire, then use that selection to display a tip to the user on how to get that particular format.


  • Try to avoid using Semicolons.  If you have to use one, explain it.
  • You are encourage to use backticks and PowerShell next-line detection (brackets, pipes) to wrap your code in to a readable one-liner
  • Interpret GUI and ‘display a tip’ however you like
  • No prizes, no whining!

17 thoughts on “Impractical One-liner Challenge #2

  1. Rohn Edwards (@magicrohn) January 23, 2015 / 5:11 pm

    How about this? (This makes an assumption about the indexes matching, so I would recommend using the Get-Date -Format ‘xxxx’ version over the GetDateTimeFormats()[x] version):

    [Globalization.DateTimeFormatInfo]::CurrentInfo.GetAllDateTimePatterns() | ForEach-Object {
    $i = 0
    $Now = Get-Date
    } {
    [PSCustomObject] @{
    Index = $i++
    FormattedString = $Now.ToString($_)
    Pattern = $_
    } | Out-GridView -Title “Choose the desired format(s)” -PassThru |
    ForEach-Object { @”
    {0} = {{
    Get-Date -Format ‘{1}’

    “@ -f $_.FormattedString, $_.Pattern, $_.Index


    • FoxDeploy January 23, 2015 / 5:36 pm

      SLICK! I never thought about incrementing the index from within an object declaration! I actually had to use a semicolon in my approach!


    • evetsleep1975 January 23, 2015 / 9:39 pm

      I like your use of [Globalization.DateTimeFormatInfo]. I don’t think I’ve used that before. It let me alter my take on the challenge to provide a more meaningful example format:

      [Globalization.DateTimeFormatInfo]::CurrentInfo.GetAllDateTimePatterns() | Sort-Object -Unique | foreach {$index = 0} { [PSCustomObject]@{
      Index = “$index.”
      Format = $_
      Example = $Now.ToString($_)
      } | Out-GridView -Title “Please select your format” -OutputMode Single | Select Index,Format,Example


      • evetsleep1975 January 23, 2015 / 9:53 pm

        Now I can’t stop myself. I like this one better because it’s almost impossible to read (as are all appropriate one-liners):

        $(for($i=0;$i -lt [Globalization.DateTimeFormatInfo]::CurrentInfo.GetAllDateTimePatterns().count;$i++){ [PSCustomObject]@{
        Example = $Now.ToString([Globalization.DateTimeFormatInfo]::CurrentInfo.GetAllDateTimePatterns()[$i])}
        ) | Out-GridView -OutputMode single -Title ‘Pick your format’


  2. Sean Quinlan January 23, 2015 / 5:32 pm

    Cool idea!

    This is the best I can come up with:

    0..(Get-Date).GetDateTimeFormats().Count |
    ForEach-Object { @{“$_” = “$((Get-Date).GetDateTimeFormats()[$_])”} } |
    Out-GridView -PassThru |
    ForEach-Object {‘Get the date format “{0}” with the following command: “(Get-Date).GetDateTimeFormats()[{1}]”‘ -f $_.Value,$_.Name }


    • FoxDeploy January 23, 2015 / 5:37 pm

      Very clever! I tried doing this angle with a for($i =0 ;$i -gt (Get-Date).GetDateTimeFormats().Count; $i++), but got burned because the For Doesn’t emit pipeline output. Very Clever.


      • evetsleep1975 January 23, 2015 / 9:49 pm

        Challenge accepted!

        $(for($i=0;$i -lt (get-date).GetDateTimeFormats().count;$i++){ [PSCustomObject]@{
        Format=(get-date).GetDateTimeFormats()[$i]} } ) | Out-GridView -OutputMode single -Title ‘Pick your format’


    • Sean Rinn January 23, 2015 / 7:01 pm

      Very weird… I came to almost the exact same solution:

      0..(((get-date).GetDateTimeFormats()).Count-1) |
      %{@{$_ = ((Get-Date).GetDateTimeFormats()[$_]).tostring()}} |
      Out-GridView -PassThru -Title “Which format(s) would you like?” |
      %{Write-Output (“`”{0}`” can be made by running: (Get-Date).GetDateTimeFormats()[{1}]” -f $_.Value,$_.Name)}


  3. mrbrandonboyd January 23, 2015 / 7:20 pm

    Loved the challenge!

    0..((Get-Date).GetDateTimeFormats().Count – 1) |
    ForEach-Object {
    New-Object -TypeName PSObject -Property @{
    Index = $_
    ‘Time Format’ = (Get-Date).GetDateTimeFormats()[$_]
    } |
    Out-GridView -PassThru |
    ForEach-Object {
    Write-Output “You’ve selected the date format: $($_.’time format’)!”

    I have a tendency to make objects rather than do string formatting which looks like the others’ used! I need to re-evaluate EVERYTHING lool


    • FoxDeploy January 23, 2015 / 9:27 pm

      I make objects too, but I use the casting constructor of [pscustomobject]@{ColumnHeading=} instead. Your method works fine still!


  4. Kevin January 23, 2015 / 7:21 pm

    I had a semicolon in the hashtable because I was defining two properties. But then I saw Sean Quinlan’s post and it made me realize that I could do it with one property.

    (Get-Date).GetDateTimeFormats() | ForEach-Object -Begin{$index = 0} {@{“$_”= $index++}} | Out-GridView -OutputMode Single | %{“(Get-Date).GetDateTimeFormats()[{0}]” -f $_.value}

    Liked by 1 person

    • FoxDeploy January 23, 2015 / 7:30 pm

      OK, you guys are all pretty much better than I am…I was previously proud of my approach to this, but after seeing what you guys can do, I’m not sure I wanna post it anymore 🙂


  5. evetsleep1975 January 23, 2015 / 8:41 pm

    This is what I came up with if I understood the rules right :). I uniqued the time formats because there looked like there were duplicates in there.

    (get-date).GetDateTimeFormats() | Get-Unique | foreach {$index=0} {
    $index++} | Out-GridView -Title ‘Please pick a date format’ -OutputMode Single | Select DateFormat


    • FoxDeploy January 23, 2015 / 8:53 pm

      Wow, I had no idea that you could do that in a foreach!


      • evetsleep1975 January 23, 2015 / 9:14 pm

        That’s the great thing about stuff like this. We all get to share 🙂 (and then come back to it a year later when we forget how to do it..I know I refer back to my scripting games submissions every now and then)


  6. jimmeh January 25, 2015 / 12:09 am

    Is this cheating? 😛

    { Write-Output “Enter number of a format you like: ” },`
    { param($formats) for ($i = 0; $i -lt $formats.Count; $i++){ “$($i): ” + $formats[$i] } },`
    { param($formats) Write-Output “You selected $($formats[(Read-Host)])!” }`
    | %{ Invoke-Command $_ -Args (,(Get-Date).GetDateTimeFormats()) }


    • FoxDeploy January 25, 2015 / 12:33 am

      I’ll have to run this in the console…


Have a code issue? Share your code by going to and pasting your code there, then post the link here!

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.