Set date format to be used in PowerShell export-csv?
powershell
Solution
try formatting your culture:
PS C:\> $(get-date).ToShortDateString()
2/16/2013
PS C:\> $(Get-Culture).DateTimeFormat.ShortDatePattern = 'yyyy-MM-dd'
PS C:\> $(get-date).ToShortDateString()
2013-02-16
Problem
I'm trying to export a database table to text (CSV-ish) for a later BULK INSERT. It would be a lot less hassle to have dates in ISO format yyyy-mm-dd. I have, I believe, finally persuaded SQL Server Express to expect British format in its import (despite the greyed out Server Properties being stuck in "English (US)" no matter what I do). I changed the user accounts to British, and that corresponds to my PowerShell CSV export format. But I'd rather use ISO format to route around the problem for good. At the moment, having filled a table variable from a `SELECT * FROM Table` and piped that into Export-CSV, the dates are coming out in the generated text file as `dd/mm/yyyy` format. How can I force the PowerShell script to use `ISO` format dates in all statements (i.e. no specifying formats in each individual command), so the Export-CSV will write them as I need? I've been going around in circles for a couple of hours looking at 'cultures' and things, but I'm utterly confused!