If it helps, I'm on PowerShell Core, v.6.0.1 or v.6.1.0. Apparently, on some versions of PowerShell the parsing of -1 results in an error, but not on either of my systems. In fact, I can get a "valid" ConsoleColor given any negative number. I'm just trying to understand why that works, and what it means.
I'm playing with a PowerShell script that lets you select from a menu of options using arrow keys: here, discussed in this question.
I notice that the script's code tries to use the current values of [Console]::BackgroundColor
and [Console]::ForegroundColor
, but in my PowerShell session, those values are returned as -1, which is not a value that can be used to set them.
I changed the script to override the values with some colors of my choice if they're not already customized, but then I wanted to also be able to reset them. [Console]::ResetColor() resets both the background and the foreground colors, so I can't use it if one of them is customized while the other is not.
However, this does work:
[Console]::ForegroundColor = [Enum]::Parse([ConsoleColor], -1)
I actually found that I could back up the original colors and assign them back with no issues, so now I'm just wondering about the behavior of this enum in general.
Why does the above work but not [Console]::ForegroundColor = -1
?