My script takes a bit of time so I am doing a Write-Host at various points just to show progress. I want to just have the time, but when I add Get-Date -DisplayHint Time to the string it outputs Date and Time (in 24 hour format as well).
If I run Get-Date -DisplayHint Time at the prompt, it works fine. If I put it in the script on its own line, it works fine.
I tried:
Write-Host "Starting Script at $(Get-Date -DisplayHint Time)"
Output:
Starting Script at 03/30/2021 14:56:20
and I tried:
$runTime = Get-Date -DisplayHint Time
$runTime
Write-Host "Starting Script @ $runTime"
Output:
11:01:09 AM
Starting Script @ 03/31/2021 11:01:09
And on it's own:
Get-Date -DisplayHint Time
Output:
10:49:24 AM
What gives?
---EDIT--- Also just tried converting the time to string like this:
(Get-Date).ToString('h:mm:ss')
$runTime = (Get-Date).ToString('h:mm:ss')
$runTime
$runTime.GetType()
Write-Host ''
Write-Host "Starting Script @ $((Get-Date).ToString('h:mm:ss'))"
Write-Host "Starting Script @ $runTime"
Output:
11:12:40
11:12:40
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
Starting Script @ 11:12:40
Starting Script @ 11:12:40
So if $runTime is now a string, why is it being run in the Write-Host?