使用Powershell导出为CSV时的日期格式

时间:2013-09-17 18:15:28

标签: datetime powershell csv

我有一个执行sql查询的powershell脚本,然后将信息导出到CSV文件。但是,CSV文件会将所有DateTimes条目重新格式化为mm/d/yyyy hh:mm:ss。我要求条目采用yyyy-mm-dd-hh.mm.ss.00

格式
$SqlCmd.CommandText = $sqlSelectCVS
$SqlCmd.Connection = $SqlConnection
$SqlAdapter.SelectCommand = $SqlCmd
$SqlAdapter.Fill($export)

#Close the SQL connection
$SqlConnection.Close()

#Creates the New file with out Column header inforamtion
$export.tables[0] |export-csv -Path ($ExportTo + '\' + $ExportName) -notypeinformation 
(Get-Content ($ExportTo + '\' + $ExportName)| Select-Object -Skip 1) | Set-Content      ($ExportTo + '\' + $ExportName)

数据库连接和查询工作正常。另外作为旁注,我试过在记事本中打开它没有运气,日期是未来的日期.. 我仍然是powershell的新手,所以如果你能解释任何建议让我理解它,我将不胜感激。

由于

1 个答案:

答案 0 :(得分:1)

您可以在PowerShell中使用.NET库来格式化字符串,如下所示:

"{0:yyyy-mm-dd-hh.hh.mm.ss.00}" -f [datetime]$stringOrWhateverDateVariable

$ stringOrWhateverDateVariable将是SQL表中的日期变量。

我无法测试SQL数据库,但它适用于DateTime格式的字符串。