Reputation: 703
I am trying to insert the timestamp into the filename of an output file generated by PowerShell scrip.
So instead of filelist.csv
, it names the output file with the actual timestamp (of the outpufile's modification time), like YYYYMMDD_HHMMSS_filelist.csv
.
Get-childitem -recurse -file | select-object @{n="Hash";e={get-filehash -algorithm MD5 -path $_.FullName | Select-object -expandproperty Hash}},lastwritetime,length,fullname | export-csv filelist.csv -notypeinformation
Any suggestions as to what is the missing code from the above line for timestamping the output file?
Upvotes: 0
Views: 31590
Reputation: 10019
Change:
export-csv filelist.csv -notypeinformation
To:
Export-Csv "$((Get-Date).ToString("yyyyMMdd_HHmmss"))_filelist.csv" -NoTypeInformation
Edit - .ToString()
and -UFormat
, generally
When using .ToString()
or -Format
/-UFormat
, you are passing a DateTime object and getting back a formatted string. In both cases you have to specify the format of the string you are after. The ways this format is specified differs.
Check out this list of .NET
format strings that can be used with ToString()
/-Format
. Note how e.g. MM
is months 01-12, M
is months 1-12 and mm
is minutes 00-59.
-UFormat
is documented to use UNIX formatting. I'm not familair with this at all, but the Notes section goes into detail. You can see here %m
is month while %M
Upvotes: 3
Reputation: 19654
In your file name, do as follows:
Export-Csv -Path "$(Get-Date -UFormat '%Y%m%d_%H%M%S')_filelist.csv" -NoTypeInformation
Upvotes: 1