Search code examples
powershellstring-interpolation

PowerShell: different result with and without string interpolation (with Get-Date -DisplayHint Time)


When I run Get-Date -DisplayHint Time, the output is 10:30:19.

When I run "$(Get-Date -DisplayHint Time)", the output is 02/15/2018 10:30:15.

Why the difference?

PSVersion = 5.1.16299.98

Solution

  • Because -DisplayHint is, well, a display hint. The result of the cmdlet is still a DateTime object. Inside a string the expression doesn't count as being "displayed", and you will get... something else. (Curiously, it's not the result of a simple .ToString()). Use "$(Get-Date -Format 'T')" if you want the locale-dependent long time format inside a string (which is apparently what -DisplayHint Time will do, although that's not explicitly documented as such).