why does adding a quotation change the expression’s value in powershell?

That’s the default format for the ToString() method in the current culture. This output is the result of “turning it into a string”. The following examples all result in similar output as they are converting to string.

Write-Host (converts the output to a string)

Write-Host (Get-Date -DisplayHint Date)

11/3/2020 11:46:00 PM

ToString()

(Get-Date -DisplayHint Date).ToString()

11/3/2020 11:48:55 PM

-as operator

(Get-Date -DisplayHint Date) -as [string]

11/03/2020 23:49:37

String interpolation

"{0}" -f (Get-Date -DisplayHint Date)

11/3/2020 11:51:27 PM

And of course you already discovered converting to string by enclosing in quotes.

If you want to control how it’s formatted, here are a few ways.

(Get-Date).ToLongDateString()

Tuesday, November 3, 2020

(Get-Date).ToString("dddd, MMMM d, yyyy")

Tuesday, November 3, 2020

Get-Date -f "dddd, MMMM d, yyyy"

Tuesday, November 3, 2020

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top