1

The following code yields the value I want.

PS> $tt = gci -Path \\Munis2\musys_read\export_test\* -Include "ARLMA_*.csv" | sort LastWriteTime -Descending
PS> $ticks = $tt[0].LastWriteTime | Format-Custom @{expr={$_.Date.Ticks};depth=1}
PS> $ticks

class DateTime
{
  $_.Date.Ticks = 637819488000000000
}

That value is $_.Date.Ticks

I have been searching for ways to extract this value, and cannot come up with a way to do it.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131
  • 2
    I asked on your other question, this might be what you're looking for `| Select-Object Name, LastWriteTime, @{n = 'Ticks';e = { $_.LastWriteTime.Date.Ticks }}` – Santiago Squarzon Mar 05 '22 at 20:45

1 Answers1

4

You're probably looking for

$ticks = $tt[0].LastWriteTime.Date.Ticks

Note: Thanks to PowerShell's member-access enumeration feature, applying .Date.Ticks to multiple input objects would work too ((...).Date.Ticks, where ... represents a command that outputs multiple [datetime] instances).

Alternatively - more slowly, but in a streaming fashion - pipe to
... | ForEach-Object { $_.Date.Ticks }.


As for what you tried:

The sole purpose of Format-* cmdlets is to output objects that provide formatting instructions to PowerShell's output-formatting system - see this answer.

In short: only ever use Format-* cmdlets to format data for display, never for subsequent programmatic processing.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • Very helpful. Now, that syntax makes sense, and you're right that I should have associated Format with display, not calculating. Thank you. – octopusgrabbus Mar 05 '22 at 20:50
  • 1
    Glad to hear it helped, @octopusgrabbus; my pleasure. Please also see my update. Santiago's comment shows you the _data_ equivalent to your approach: using _`Select-Object`_ with a [calculated property](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Calculated_Properties). – mklement0 Mar 05 '22 at 20:57