Q: Why does adding quotation changes the expression's value in Powershell? I have just started to learn Powershell, and there is one thing I just can't wrap my head around. See below:
> Write-output $(Get-Date -DisplayHint Date)  
    //Expect: Tuesday, November 3, 2020
    //Got: Tuesday, November 3, 2020

> Write-output "$(Get-Date -DisplayHint Date)" 
    //Expect: Tuesday, November 3, 2020
    //Got: 11/03/2020 20:42:27

For the second line with quotation interpolation, can someone explain why it is showing a different format?
Thank you,

A: 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


A: Date can be showed in a customised way when it is converted into string.
In the above code you have enclosed in double quotes, which powershell treats it as a string.
(Get-Date -DisplayHint Date).toString() may also give you same result.
