If I have:
Write-Host "[$(Get-Date -displayhint time)] backup starting..."
I get:
[02/17/2010 1:26:12pm] backup starting...
i. e. the Get-Date parameters are being ignored and is just returning the output of Get-Date.
What's the best way to do inject the current time in the middle of a string?
thanks
Well, in this case you're converting to a string because you are using the output in a string. The result of the Get-Date command is still a DateTime object. The display hint would then be honored by the Out-Host cmdlet.
You can use the -Format parameter to force a certain format in which case the cmdlet returns a string:
Get-Date -Format T
("T" being the format string for the full time) which then looks like this:
PS Home:\> Write-Host "[$(Get-Date -Format T)] backup starting..."
[19:35:12] backup starting...
You can also use the ToString() method of the DateTime class. I usually do something like this:
[PS] C:\>write-host "$((get-date).tostring('yyyy.MM.dd-HH:mm:ss'))"
2010.02.19-09:54:51
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论