'Powershell: How To Extract Binary File Attributes From Directory Listing

In PowerShell, I can get a nice list of files in descending sorted order using a filter:

$tt = gci -Path \\Munis2\musys_read\export_test\* -Include "ARLMA_*.csv" | sort LastWriteTime -Descending

PS H:\WindowsPowerShell\Scripts\ProductionScripts\Munis> $tt


    Directory: \\Munis2\musys_read\export_test


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----        03/04/2022   3:09 AM       25545520 ARLMA_20220304030027.csv
.
.
.

Then, I can get just the name of the file for the purposes of transferring that file to an FTP site.

PS H:\WindowsPowerShell\Scripts\ProductionScripts\Munis> $tt[0].Name
ARLMA_20220304030027.csv

How can I parse $tt[0].LastWriteTime

PS H:\WindowsPowerShell\Scripts\ProductionScripts\Munis> $tt[0].LastWriteTime

Friday, March 4, 2022 3:09:14 AM

into something that looks like yymmddhhmmss, or is there a way to get the binary time of the file the last time it was accessed?



Solution 1:[1]

The ToString() method can be used to format the date into a string. Are you sure that a two digit year is appropriate?

$DateResult = (Get-ChildItem -Path \\Munis2\musys_read\export_test\* -Include "ARLMA_*.csv" |
    Sort-Object -Property LastWriteTime -Descending |
    Select-Object -First 1).LastWriteTime.ToString('yyMMddHHmmss')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 lit