I have a .ps1 script that stores Get-Date
in a $variable
on startup and then at the end substracts that from Get-Date
again to show how long the script ran.
Although it looks OK to me, it throws this error when I run it from PS7.
What's weird is that when I put parenthesis around Get-Date
in the subtraction it does work.
PS C:\git> $test = Get-Date
PS C:\git> "Operation took {0}" -f (Get-Date - $test)
Get-Date: Cannot bind parameter 'Date'. Cannot convert value "-" to type "System.DateTime". Error: "String '-' was not recognized as a valid DateTime."
PS C:\git> "Operation took {0}" -f ((Get-Date) - $test)
Operation took 00:00:24.4198449
I don't get the 'thinking' behind why this would behave differently!??
PS: this is on version 7.2.2 (win10) and seems to be true on 5.1.19041.1320 too.