Haggan
Haggan

Reputation: 79

Add timestamp in Powershell output

I have the following code that export a CSV-file from a query. How do I add a "Get-date" (today) stamp in the end of the file in the filepath?

$ds.Tables[0] | Export-csv -delimiter ";" -path "C:\temp\TempWarehousingControl.csv" -NoTypeInformation -Force -Encoding UTF8 
Get-Content C:\temp\TempWarehousingControl.csv | %{ $_ -replace """""", "NULL"} |%{ $_ -replace """", ""} | out-file -FilePath C:\Batch\Output\WarehousingControl\WarehousingControl.csv -Force -Encoding utf8

Upvotes: 0

Views: 1905

Answers (2)

Mark Wragg
Mark Wragg

Reputation: 23355

Assuming that you meant "how do I add today's date to the end of the filepath" (vs the end of the file), this will do it:

"C:\Batch\Output\WarehousingControl\WarehousingControl-$(get-date -Format 'dd-MM-yyyy').csv"

Included in your full code:

$ds.Tables[0] | Export-csv -delimiter ";" -path "C:\temp\TempWarehousingControl.csv" -NoTypeInformation -Force -Encoding UTF8 
Get-Content C:\temp\TempWarehousingControl.csv | %{ $_ -replace """""", "NULL"} |%{ $_ -replace """", ""} | out-file -FilePath "C:\Batch\Output\WarehousingControl\WarehousingControl-$(get-date -Format 'dd-MM-yyyy').csv" -Force -Encoding utf8

Explanation:

This is a subexpression $() which uses get-date to get the current date and formats it as dd-MM-yyyy (note month is a capital M as lowercase m is the code for minute).

Upvotes: 1

David Brabant
David Brabant

Reputation: 43499

$path = "C:\Batch\Output\WarehousingControl\WarehousingControl.csv"
$path = $path.Replace(".csv", "_" + (Get-Date -f "yyy-MM-dd") + ".csv")

$ds.Tables[0] | 
    Export-csv -delimiter ";" -path "C:\temp\TempWarehousingControl.csv" -NoTypeInformation -Force -Encoding UTF8 

Get-Content C:\temp\TempWarehousingControl.csv | 
    %{ $_ -replace """""", "NULL"} |
    %{ $_ -replace """", ""} | 
    Out-File -FilePath $path -Force -Encoding utf8

Upvotes: 0

Related Questions