octopusgrabbus
octopusgrabbus

Reputation: 10685

Powershell How to extract class member's value

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.

Upvotes: 1

Views: 197

Answers (1)

mklement0
mklement0

Reputation: 437198

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.

Upvotes: 4

Related Questions