Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391396

"true" messes up escape code handling in Powershell?

I was observing something strange in my Powershell script just now and I finally narrowed it down to a bug in my code, it was this line:

$AnyExecuted = true

It should've been

$AnyExecuted = $true

However, this line messes up escape code handling in both the standaline Powershell Core windows on Windows 10, as well as in the Windows Terminal:

messed up prompt

Anyone know why this happens? What is the true expression? I can't find any mention of it, and why would it have this effect on the output from Powershell?

Note, obviously I updated my script to use $true, which was what I intended, this question is just about what true is.

Upvotes: 2

Views: 65

Answers (1)

stackprotector
stackprotector

Reputation: 13460

When executing

$AnyExecuted = $true

$AnyExecuted will be of type Boolean and your script may run as expected. If you execute

$AnyExecuted = true

instead and no error is thrown, then there is an alias, cmdlet or executable available that has the name true. It will not be available on default Windows 10 installations.

As it does not throw an error on your side, you might have true somehow available. You can check for that with Get-Command true. Using a MinGW environment, makes true.exe available, for example.

Using the result of your still unknown true "thing" may indeed lead to unexpected behaviour. To investigate on the result of true you can evaluate (true).GetType() on your system.

Upvotes: 6

Related Questions