1

We have a Windows Server with the Region settings for short dates set to dd.MM.yyyy. However powershell outputs the dates as MM/dd/yyyy:

$d = (Get-Item .\somefile.txt).CreationTime
Write-Output "$d" # => 09/26/2016 15:35:35

Also, the toString() function returns a different (correct) format

Write-Output "$($d.toString())" # => 26.09.2016 15:35:35

Questions:

  1. Why does powershell use MM/dd/yyyy?
  2. Why are the 2 formats above different?
  3. I know we can set the format in our powershell profile but is there no "System" setting which determines it?
Marc
  • 13,011
  • 11
  • 78
  • 98

1 Answers1

1

Scripts are often used for automation, rarely for interaction with users or creating UIs. The automatic conversion to string that happens when you put a variable inside a string, e.g. "$d" will always use the invariant culture and never the user's preference. Same goes for numbers, for example. This is precisely to avoid issues that arise where a string would contain a different format for a different user or on a different machine.

If you need control over the format, convert to string explicitly, not implicitly.

The same holds for parsing, incidentally. You can cast a string to a datetime, or number, but this requires a certain format to work. If you want to use the user's preference, then use [DateTime]::Parse instead.

Joey
  • 344,408
  • 85
  • 689
  • 683
  • Indeed, it's more about other systems understanding the data, not users. I guess it's a US-centric thing since `MM/dd/yyyy` ain't really culture-independent. Most systems (esp. databases) will cope well with the unambiguous `yyyy-MM-dd` as opposed to `MM/dd/yyyy` which is ambiguous. – Marc Oct 25 '16 at 19:04
  • 1
    @Marc: I (as a German) also disagree with the choice of invariant date format. However, the nice thing is, you can use ISO-8601 just fine when casting a string to a `DateTime`. Also, if you use format strings, or an explicit `ToString()` call, instead of string interpolation, or casting, you'll get the user's preferred format. – Joey Oct 28 '16 at 15:10