0

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?

1 Answers1

0

You need to convert your datetime object to a string in order to display it correctly.

I personally have never found Write-Host useful, for your specific purpose here, is not needed at all. This is of course open to debate and I'm not interested in debating this, at all.

If you're interested on reading more about when it can be useful (if ever), I encourage you to read this: Write-Host vs Write-Information in PowerShell 5

There are more articles related, also some of them show when it can be useful, the only examples I could find is when you want to display colors in the screen :)

PS /~>  $runTime = Get-Date -DisplayHint Time

PS /~>  $runTime

3:45:49 PM

PS /~>  $runTime.ToString('h:mm:ss tt')
3:45:49 PM

PS /~>  $runTime.addhours(-5).ToString('h:mm:ss tt')
10:45:49 AM

PS /~>  "Starting Script @ $($runTime.ToString('h:mm:ss tt'))"
Starting Script @ 3:45:49 PM

PS /~>  "Starting Script @ {0}" -f $runTime.ToString('h:mm:ss tt')
Starting Script @ 3:45:49 PM

# Edit: Adding a few more examples

PS /~> "Starting Script @ {0:h:mm:ss tt}" -f $runTime
Starting Script @ 3:45:49 PM

PS /~> $runTime = (Get-Date).ToString('h:mm:ss tt')

PS /~> "Starting Script @ $runTime"
Starting Script @ 4:18:36 PM

PS /~> $runTime = [datetime]::Now.ToString('h:mm:ss tt')

PS /~> "Starting Script @ $runTime"
Starting Script @ 4:18:36 PM
Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
  • Using Write-Host is not bad. Just like any tool, it has it's place. – Doug Maurer Mar 31 '21 at 18:49
  • @DougMaurer Yes it is. It writes to a completely different stream (if you're using v5 or later, before that there was no way to capture it). Powershell still doesn't allow redirection of streams other than the error stream. If you're outting something, it needs to be as an object on the out stream. 'Write-Host' is a redundant cmdlet that never should have been provided in the first place. With the verbose, out, and debug streams, there's no purpose for `Write-Host`. It still exists for backwards compatibility, but should be forgotten. – Colyn1337 Mar 31 '21 at 19:01
  • 1
    I personally have never found `Write-Host` useful but everyone can have have their own opinion. The only usefulness I see in `Write-Host` is for those who want to display pretty colors in the screen... – Santiago Squarzon Mar 31 '21 at 19:09
  • Ok, you are saying to just put the string by itself on the line with no cmdlet. Correct? So what I was missing is the ToString, in the string itself. It is not good enough to convert it into the $runTime variable. Do I have that right? – WinArchitect Mar 31 '21 at 19:11
  • @WinArchitect yeah, ofc you can do that as long as the datetime object is converted to string. By the way, I see you were already doing it correctly just that you were using `'h:mm:ss'` instead of `'h:mm:ss tt'` for AM/PM. Check out my edit, there are more examples of how to do the same thing. – Santiago Squarzon Mar 31 '21 at 19:21
  • If you aren’t going to explain why you are encouraging not to use it, I strongly recommend you just remove that line. – Doug Maurer Mar 31 '21 at 19:27
  • @DougMaurer there you go, answer updated :) – Santiago Squarzon Mar 31 '21 at 19:41
  • So if I put "Processed page at $((Get-Date).ToString('h:mm:ss tt'))" in an IF statement, it doesn't write to console? Is there something special needed there? – WinArchitect Apr 01 '21 at 18:20