In PowerShell, I can use Expanding strings to insert variable values into strings (Answer with more details about that):
PS C:\> $UtcNow = [DateTime]::UtcNow
PS C:\> "UtcNow = $UtcNow"
UtcNow = 08/10/2020 09:31:51
I can do it less conveniently using String.Format
or the -f
operator, and then I can provide format specifiers:
PS C:\> "UtcNow = {0:u}" -f $UtcNow
UtcNow = 2020-08-10 09:31:51Z
PS C:\> [string]::Format("UtcNow = {0:u}", $UtcNow)
UtcNow = 2020-08-10 09:31:51Z
Question: Is there a way to use a format specifier in and expanding string?
What I've tried
PS C:\> "UtcNow = $UtcNow:u" # Doesn't work
UtcNow =
PS C:\> "UtcNow = ${UtcNow:u}" # Doesn't work
UtcNow =
PS C:\> "UtcNow = $($UtcNow.ToString("u"))" # Works, but is quite cumbersome
UtcNow = 2020-08-10 09:31:51Z