Home > Back-end >  Powershell How to extract class member's value
Powershell How to extract class member's value

Time:03-06

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.

CodePudding user response:

You're probably looking for

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

Note: Thanks to PowerShell's member 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.

  • Related